2014-12-15 22:58:49 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2015-01-05 18:18:29 +08:00
|
|
|
|
using System.Reflection;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
using System.Runtime.Serialization;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using System.Runtime.Serialization.Formatters;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
2014-12-21 20:44:31 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
|
|
|
|
|
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
|
2014-12-15 22:58:49 +08:00
|
|
|
|
/// </summary>
|
2016-04-27 09:15:53 +08:00
|
|
|
|
public class BinaryStorage<T> : Storage<T> where T : new()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public BinaryStorage()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-27 09:15:53 +08:00
|
|
|
|
FileSuffix = ".dat";
|
|
|
|
|
DirectoryName = "Cache";
|
|
|
|
|
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName);
|
|
|
|
|
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
|
|
|
|
|
|
|
|
|
|
ValidateDirectory();
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-27 09:15:53 +08:00
|
|
|
|
public override T Load()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
if (File.Exists(FilePath))
|
|
|
|
|
{
|
|
|
|
|
using (var stream = new FileStream(FilePath, FileMode.Open))
|
2015-01-04 18:14:50 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
if (stream.Length > 0)
|
2015-01-05 22:41:17 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
Deserialize(stream);
|
2015-01-06 18:28:23 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
LoadDefault();
|
2015-01-04 18:14:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
2016-04-21 08:53:21 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LoadDefault();
|
|
|
|
|
}
|
2016-04-27 09:15:53 +08:00
|
|
|
|
return Data;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2016-04-27 09:15:53 +08:00
|
|
|
|
Data = (T)binaryFormatter.Deserialize(stream);
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
catch (SerializationException e)
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(e);
|
2016-04-27 09:15:53 +08:00
|
|
|
|
LoadDefault();
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
catch (InvalidCastException e)
|
|
|
|
|
{
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(e);
|
2016-04-27 09:15:53 +08:00
|
|
|
|
LoadDefault();
|
2015-01-05 18:18:29 +08:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-27 09:15:53 +08:00
|
|
|
|
public override void LoadDefault()
|
2016-04-21 08:53:21 +08:00
|
|
|
|
{
|
2016-04-27 09:15:53 +08:00
|
|
|
|
Data = new T();
|
|
|
|
|
Save();
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 05:34:42 +08:00
|
|
|
|
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
2015-01-05 18:18:29 +08:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
2015-01-05 18:18:29 +08:00
|
|
|
|
return ayResult;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-27 09:15:53 +08:00
|
|
|
|
public override void Save()
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
using (var stream = new FileStream(FilePath, FileMode.Create))
|
2014-12-21 20:44:31 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
BinaryFormatter binaryFormatter = new BinaryFormatter
|
2015-01-05 22:41:17 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
AssemblyFormat = FormatterAssemblyStyle.Simple
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-04-27 09:15:53 +08:00
|
|
|
|
binaryFormatter.Serialize(stream, Data);
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
catch (SerializationException e)
|
|
|
|
|
{
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(e);
|
2015-01-23 21:52:46 +08:00
|
|
|
|
}
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|