use %APPDATA%

1. Fix can't find Result.ctor bug for plugin introduced in
c0889de1f9ae460b2cc189eb59e5bd90ddb7d17e
2. use %APPDATA% for all data, part of #389
3. MISC
This commit is contained in:
bao-qian
2016-04-27 02:15:53 +01:00
parent e96bd5a0e7
commit dc3b01dc15
22 changed files with 157 additions and 159 deletions

View File

@@ -13,30 +13,20 @@ namespace Wox.Infrastructure.Storage
/// Normally, it has better performance, but not readable
/// You MUST mark implement class as Serializable
/// </summary>
public class BinaryStorage<T> where T : class, new()
public class BinaryStorage<T> : Storage<T> where T : new()
{
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()
{
FileName = typeof(T).Name;
DirectoryPath = Path.Combine(WoxDirectroy.Executable, DirectoryName);
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix); ;
FileSuffix = ".dat";
DirectoryName = "Cache";
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName);
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
ValidateDirectory();
}
public T Load()
public override T Load()
{
if (!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
}
if (File.Exists(FilePath))
{
using (var stream = new FileStream(FilePath, FileMode.Open))
@@ -55,7 +45,7 @@ namespace Wox.Infrastructure.Storage
{
LoadDefault();
}
return _binary;
return Data;
}
private void Deserialize(FileStream stream)
@@ -69,17 +59,17 @@ namespace Wox.Infrastructure.Storage
try
{
_binary = (T)binaryFormatter.Deserialize(stream);
Data = (T)binaryFormatter.Deserialize(stream);
}
catch (SerializationException e)
{
LoadDefault();
Log.Error(e);
LoadDefault();
}
catch (InvalidCastException e)
{
LoadDefault();
Log.Error(e);
LoadDefault();
}
finally
{
@@ -87,9 +77,10 @@ namespace Wox.Infrastructure.Storage
}
}
private void LoadDefault()
public override void LoadDefault()
{
_binary = new T();
Data = new T();
Save();
}
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
@@ -108,7 +99,7 @@ namespace Wox.Infrastructure.Storage
return ayResult;
}
public void Save()
public override void Save()
{
using (var stream = new FileStream(FilePath, FileMode.Create))
{
@@ -119,7 +110,7 @@ namespace Wox.Infrastructure.Storage
try
{
binaryFormatter.Serialize(stream, _binary);
binaryFormatter.Serialize(stream, Data);
}
catch (SerializationException e)
{

View File

@@ -7,23 +7,19 @@ namespace Wox.Infrastructure.Storage
/// <summary>
/// Serialize object using json format.
/// </summary>
public class JsonStrorage<T> where T : new()
public class JsonStrorage<T> : Storage<T> where T : new()
{
private T _json;
private readonly JsonSerializerSettings _serializerSettings;
protected string FileName { get; set; }
protected string FilePath { get; set; }
protected const string FileSuffix = ".json";
protected string DirectoryPath { get; set; }
protected const string DirectoryName = "Config";
internal JsonStrorage()
{
FileName = typeof(T).Name;
DirectoryPath = Path.Combine(WoxDirectroy.Executable, DirectoryName);
FileSuffix = ".json";
DirectoryName = "Settings";
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName);
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
ValidateDirectory();
// use property initialization instead of DefaultValueAttribute
// easier and flexible for default value of object
_serializerSettings = new JsonSerializerSettings
@@ -33,13 +29,8 @@ namespace Wox.Infrastructure.Storage
};
}
public T Load()
public override T Load()
{
if (!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
}
if (File.Exists(FilePath))
{
var searlized = File.ReadAllText(FilePath);
@@ -56,33 +47,31 @@ namespace Wox.Infrastructure.Storage
{
LoadDefault();
}
return _json;
return Data;
}
private void Deserialize(string searlized)
{
try
{
_json = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
Data = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
}
catch (JsonSerializationException e)
{
LoadDefault();
Log.Error(e);
}
}
private void LoadDefault()
public override void LoadDefault()
{
_json = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
Data = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
Save();
}
public void Save()
public override void Save()
{
string serialized = JsonConvert.SerializeObject(_json, Formatting.Indented);
string serialized = JsonConvert.SerializeObject(Data, Formatting.Indented);
File.WriteAllText(FilePath, serialized);
}
}

View File

@@ -6,15 +6,14 @@ namespace Wox.Infrastructure.Storage
{
public PluginJsonStorage()
{
var pluginDirectoryName = "Plugins";
DirectoryName = "Plugins";
// C# releated, add python releated below
var type = typeof (T);
FileName = type.Name;
var assemblyName = type.Assembly.GetName().Name;
DirectoryPath = Path.Combine(WoxDirectroy.Executable, DirectoryName, pluginDirectoryName, assemblyName);
var assemblyName = DataType.Assembly.GetName().Name;
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName, assemblyName);
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);
ValidateDirectory();
}
}
}

View File

@@ -0,0 +1,46 @@
using System;
using System.IO;
namespace Wox.Infrastructure.Storage
{
public class Storage<T>
{
protected T Data;
protected Type DataType { get; }
public string FileName { get; }
public string FilePath { get; set; }
public string FileSuffix { get; set; }
public string DirectoryPath { get; set; }
public string DirectoryName { get; set; }
public virtual T Load()
{
throw new NotImplementedException();
}
public virtual void Save()
{
throw new NotImplementedException();
}
public virtual void LoadDefault()
{
throw new NotImplementedException();
}
protected Storage()
{
DataType = typeof (T);
FileName = DataType.Name;
DirectoryPath = Wox.DataPath;
}
protected void ValidateDirectory()
{
if (!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
}
}
}
}