mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 03:36:44 +02:00
Remove instance logic for BinaryStorage and JsonStorage, part 1
1. part of #389 2. huge refactoring
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Threading;
|
||||
using Wox.Infrastructure.Logger;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
@@ -13,55 +13,73 @@ namespace Wox.Infrastructure.Storage
|
||||
/// Normally, it has better performance, but not readable
|
||||
/// You MUST mark implement class as Serializable
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public abstract class BinaryStorage<T> : BaseStorage<T> where T : class, IStorage, new()
|
||||
public class BinaryStorage<T> where T : class, new()
|
||||
{
|
||||
private static object syncObject = new object();
|
||||
protected override string FileSuffix
|
||||
private T _binary;
|
||||
|
||||
private string FilePath { get; }
|
||||
private string FileName { get; }
|
||||
private const string FileSuffix = ".dat";
|
||||
private string DirectoryPath { get; }
|
||||
private const string DirectoryName = "Config";
|
||||
|
||||
public BinaryStorage()
|
||||
{
|
||||
get { return ".dat"; }
|
||||
FileName = typeof(T).Name;
|
||||
DirectoryPath = Path.Combine(WoxDirectroy.Executable, DirectoryName);
|
||||
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix); ;
|
||||
}
|
||||
|
||||
protected override void LoadInternal()
|
||||
public T Load()
|
||||
{
|
||||
//http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception
|
||||
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
||||
try
|
||||
if (!Directory.Exists(DirectoryPath))
|
||||
{
|
||||
using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
||||
Directory.CreateDirectory(DirectoryPath);
|
||||
}
|
||||
|
||||
if (File.Exists(FilePath))
|
||||
{
|
||||
using (var stream = new FileStream(FilePath, FileMode.Open))
|
||||
{
|
||||
if (fileStream.Length > 0)
|
||||
if (stream.Length > 0)
|
||||
{
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter
|
||||
{
|
||||
AssemblyFormat = FormatterAssemblyStyle.Simple
|
||||
};
|
||||
serializedObject = binaryFormatter.Deserialize(fileStream) as T;
|
||||
if (serializedObject == null)
|
||||
{
|
||||
serializedObject = LoadDefault();
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new System.Exception("deserialize failed");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Deserialize(stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
serializedObject = LoadDefault();
|
||||
LoadDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
else
|
||||
{
|
||||
LoadDefault();
|
||||
}
|
||||
return _binary;
|
||||
}
|
||||
|
||||
private void Deserialize(FileStream stream)
|
||||
{
|
||||
//http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception
|
||||
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter
|
||||
{
|
||||
AssemblyFormat = FormatterAssemblyStyle.Simple
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
_binary = (T)binaryFormatter.Deserialize(stream);
|
||||
}
|
||||
catch (SerializationException e)
|
||||
{
|
||||
Log.Error(e);
|
||||
serializedObject = LoadDefault();
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
LoadDefault();
|
||||
}
|
||||
catch (InvalidCastException e)
|
||||
{
|
||||
Log.Error(e);
|
||||
LoadDefault();
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -69,6 +87,11 @@ namespace Wox.Infrastructure.Storage
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadDefault()
|
||||
{
|
||||
_binary = new T();
|
||||
}
|
||||
|
||||
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
||||
{
|
||||
Assembly ayResult = null;
|
||||
@@ -85,33 +108,24 @@ namespace Wox.Infrastructure.Storage
|
||||
return ayResult;
|
||||
}
|
||||
|
||||
protected override void SaveInternal()
|
||||
public void Save()
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
using (var stream = new FileStream(FilePath, FileMode.Create))
|
||||
{
|
||||
lock (syncObject)
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream fileStream = new FileStream(FilePath, FileMode.Create);
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter
|
||||
{
|
||||
AssemblyFormat = FormatterAssemblyStyle.Simple
|
||||
};
|
||||
binaryFormatter.Serialize(fileStream, serializedObject);
|
||||
fileStream.Close();
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
AssemblyFormat = FormatterAssemblyStyle.Simple
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
binaryFormatter.Serialize(stream, _binary);
|
||||
}
|
||||
});
|
||||
catch (SerializationException e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user