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
|
|
|
|
|
/// </summary>
|
2017-02-07 08:21:39 +08:00
|
|
|
|
public class BinaryStorage<T>
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2017-01-13 23:40:32 +08:00
|
|
|
|
public BinaryStorage(string filename)
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2017-02-07 08:21:39 +08:00
|
|
|
|
const string directoryName = "Cache";
|
|
|
|
|
var directoryPath = Path.Combine(Constant.DataDirectory, directoryName);
|
|
|
|
|
Helper.ValidateDirectory(directoryPath);
|
2016-04-27 09:15:53 +08:00
|
|
|
|
|
2017-02-07 08:21:39 +08:00
|
|
|
|
const string fileSuffix = ".cache";
|
|
|
|
|
FilePath = Path.Combine(directoryPath, $"{filename}{fileSuffix}");
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 08:21:39 +08:00
|
|
|
|
public string FilePath { get; }
|
|
|
|
|
|
2017-01-13 23:40:32 +08:00
|
|
|
|
public T TryLoad(T defaultData)
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
if (File.Exists(FilePath))
|
|
|
|
|
{
|
2017-12-22 23:59:55 +08:00
|
|
|
|
if (new FileInfo(FilePath).Length == 0)
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"|BinaryStorage.TryLoad|Zero length cache file <{FilePath}>");
|
|
|
|
|
Save(defaultData);
|
|
|
|
|
return defaultData;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
using (var stream = new FileStream(FilePath, FileMode.Open))
|
2015-01-04 18:14:50 +08:00
|
|
|
|
{
|
2017-12-22 23:59:55 +08:00
|
|
|
|
var d = Deserialize(stream, defaultData);
|
|
|
|
|
return d;
|
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
|
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
Log.Info("|BinaryStorage.TryLoad|Cache file not exist, load default data");
|
2017-01-13 23:40:32 +08:00
|
|
|
|
Save(defaultData);
|
|
|
|
|
return defaultData;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 23:40:32 +08:00
|
|
|
|
private T Deserialize(FileStream stream, T defaultData)
|
2016-04-21 08:53:21 +08:00
|
|
|
|
{
|
|
|
|
|
//http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
|
|
|
|
BinaryFormatter binaryFormatter = new BinaryFormatter
|
|
|
|
|
{
|
|
|
|
|
AssemblyFormat = FormatterAssemblyStyle.Simple
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-02-13 17:38:48 +08:00
|
|
|
|
var t = ((T)binaryFormatter.Deserialize(stream)).NonNull();
|
2017-01-13 23:40:32 +08:00
|
|
|
|
return t;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
2016-08-20 08:02:20 +08:00
|
|
|
|
catch (System.Exception e)
|
2016-04-21 08:53:21 +08:00
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
Log.Exception($"|BinaryStorage.Deserialize|Deserialize error for file <{FilePath}>", e);
|
2017-01-13 23:40:32 +08:00
|
|
|
|
return defaultData;
|
2015-01-05 18:18:29 +08:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 23:40:32 +08:00
|
|
|
|
public void Save(T data)
|
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
|
|
|
|
|
{
|
2017-01-13 23:40:32 +08:00
|
|
|
|
binaryFormatter.Serialize(stream, data);
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
catch (SerializationException e)
|
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
Log.Exception($"|BinaryStorage.Save|serialize error for file <{FilePath}>", 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|