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>
|
2017-01-13 23:40:32 +08:00
|
|
|
|
public class BinaryStorage<T> : Storage<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-01-13 23:40:32 +08:00
|
|
|
|
FileSuffix = ".cache";
|
2016-04-27 09:15:53 +08:00
|
|
|
|
DirectoryName = "Cache";
|
|
|
|
|
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName);
|
2017-01-13 23:40:32 +08:00
|
|
|
|
FileName = filename;
|
2016-04-27 09:15:53 +08:00
|
|
|
|
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
|
|
|
|
|
|
|
|
|
|
ValidateDirectory();
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
{
|
2017-01-13 23:40:32 +08:00
|
|
|
|
var d = Deserialize(stream, defaultData);
|
|
|
|
|
return d;
|
2015-01-06 18:28:23 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-01-14 22:21:02 +08:00
|
|
|
|
Log.Error($"Zero length cache file: {FilePath}");
|
2017-01-13 23:40:32 +08:00
|
|
|
|
Save(defaultData);
|
|
|
|
|
return defaultData;
|
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-14 22:21:02 +08:00
|
|
|
|
Log.Info("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-01-13 23:40:32 +08:00
|
|
|
|
var t = (T) binaryFormatter.Deserialize(stream);
|
|
|
|
|
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-14 22:21:02 +08:00
|
|
|
|
Log.Error($"Deserialize error for cache file: {FilePath}");
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(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-14 22:21:02 +08:00
|
|
|
|
Log.Error($"Serialize error for cache file: {FilePath}");
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|