PowerToys/Wox.Infrastructure/Storage/BinaryStorage.cs

118 lines
3.9 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
2015-01-23 21:52:46 +08:00
using System.Threading;
using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure.Storage
{
/// <summary>
/// Stroage object using binary data
/// Normally, it has better performance, but not readable
2014-12-18 19:22:47 +08:00
/// You MUST mark implement class as Serializable
/// </summary>
[Serializable]
public abstract class BinaryStorage<T> : BaseStorage<T> where T : class, IStorage, new()
{
2015-01-23 21:52:46 +08:00
private static object syncObject = new object();
protected override string FileSuffix
{
get { return ".dat"; }
}
protected override void LoadInternal()
{
//http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
try
{
2016-01-07 10:31:17 +08:00
using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
2015-01-06 18:28:23 +08:00
if (fileStream.Length > 0)
{
2015-01-06 18:28:23 +08:00
BinaryFormatter binaryFormatter = new BinaryFormatter
{
2015-01-06 18:28:23 +08:00
AssemblyFormat = FormatterAssemblyStyle.Simple
};
serializedObject = binaryFormatter.Deserialize(fileStream) as T;
if (serializedObject == null)
{
serializedObject = LoadDefault();
#if (DEBUG)
{
2015-11-09 09:32:33 +08:00
throw new System.Exception("deserialize failed");
2015-01-06 18:28:23 +08:00
}
#endif
2015-01-06 18:28:23 +08:00
}
}
else
{
serializedObject = LoadDefault();
}
}
}
2015-11-09 09:32:33 +08:00
catch (System.Exception e)
{
2015-02-10 23:48:09 +08:00
Log.Error(e);
serializedObject = LoadDefault();
#if (DEBUG)
{
throw;
}
#endif
}
finally
{
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
}
}
2016-01-07 05:34:42 +08:00
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Assembly ayResult = null;
string sShortAssemblyName = args.Name.Split(',')[0];
Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly ayAssembly in ayAssemblies)
{
if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0])
{
ayResult = ayAssembly;
break;
}
}
return ayResult;
}
protected override void SaveInternal()
{
2015-01-23 21:52:46 +08:00
ThreadPool.QueueUserWorkItem(o =>
{
2015-01-23 21:52:46 +08:00
lock (syncObject)
{
2015-01-23 21:52:46 +08:00
try
{
2016-01-07 10:31:17 +08:00
FileStream fileStream = new FileStream(FilePath, FileMode.Create);
2015-01-23 21:52:46 +08:00
BinaryFormatter binaryFormatter = new BinaryFormatter
{
AssemblyFormat = FormatterAssemblyStyle.Simple
};
binaryFormatter.Serialize(fileStream, serializedObject);
fileStream.Close();
}
2015-11-09 09:32:33 +08:00
catch (System.Exception e)
2015-01-23 21:52:46 +08:00
{
Log.Error(e);
#if (DEBUG)
2015-01-23 21:52:46 +08:00
{
throw;
}
#endif
2015-01-23 21:52:46 +08:00
}
}
});
}
}
}