mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
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:
@@ -94,7 +94,7 @@ namespace Wox.Infrastructure.Image
|
||||
ImageSource image = null;
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
path = Path.Combine(WoxDirectroy.Executable, "Images", "app.png");
|
||||
path = Path.Combine(Wox.ProgramPath, "Images", "app.png");
|
||||
image = new BitmapImage(new Uri(path));
|
||||
return image;
|
||||
}
|
||||
@@ -131,14 +131,14 @@ namespace Wox.Infrastructure.Image
|
||||
}
|
||||
else
|
||||
{
|
||||
path = Path.Combine(WoxDirectroy.Executable, "Images", Path.GetFileName(path));
|
||||
path = Path.Combine(Wox.ProgramPath, "Images", Path.GetFileName(path));
|
||||
if (File.Exists(path))
|
||||
{
|
||||
image = new BitmapImage(new Uri(path));
|
||||
}
|
||||
else
|
||||
{
|
||||
path = Path.Combine(WoxDirectroy.Executable, "Images", "app.png");
|
||||
path = Path.Combine(Wox.ProgramPath, "Images", "app.png");
|
||||
image = new BitmapImage(new Uri(path));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
46
Wox.Infrastructure/Storage/Storage.cs
Normal file
46
Wox.Infrastructure/Storage/Storage.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,8 +78,8 @@
|
||||
<Compile Include="Image\ImageLoader.cs" />
|
||||
<Compile Include="Logger\Log.cs" />
|
||||
<Compile Include="Storage\PluginSettingsStorage.cs" />
|
||||
<Compile Include="Storage\Storage.cs" />
|
||||
<Compile Include="SyntaxSuger.cs" />
|
||||
<Compile Include="WoxDirectroy.cs" />
|
||||
<Compile Include="Stopwatch.cs" />
|
||||
<Compile Include="Storage\BinaryStorage.cs" />
|
||||
<Compile Include="Storage\JsonStorage.cs" />
|
||||
@@ -90,6 +90,7 @@
|
||||
<Compile Include="Hotkey\HotkeyModel.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Alphabet.cs" />
|
||||
<Compile Include="Wox.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Wox.Plugin\Wox.Plugin.csproj">
|
||||
|
||||
13
Wox.Infrastructure/Wox.cs
Normal file
13
Wox.Infrastructure/Wox.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Wox.Infrastructure
|
||||
{
|
||||
public static class Wox
|
||||
{
|
||||
public const string Name = "Wox";
|
||||
public static readonly string ProgramPath = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString();
|
||||
public static readonly string DataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Name);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Wox.Infrastructure
|
||||
{
|
||||
public static class WoxDirectroy
|
||||
{
|
||||
public static string Executable { get; internal set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user