mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
fix #185. Loading index cache on startup.
This commit is contained in:
@@ -2,90 +2,92 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
public abstract class BaseStorage<T> where T : class, new()
|
||||
[Serializable]
|
||||
public abstract class BaseStorage<T> : IStorage where T : class,IStorage,new()
|
||||
{
|
||||
private string configFolder = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config");
|
||||
private string fileSuffix = ".json";
|
||||
private static object locker = new object();
|
||||
private static T storage;
|
||||
private readonly string configFolder = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config");
|
||||
|
||||
public event Action<T> AfterLoadConfig;
|
||||
|
||||
protected virtual void OnAfterLoadConfig(T obj)
|
||||
protected string ConfigPath
|
||||
{
|
||||
Action<T> handler = AfterLoadConfig;
|
||||
if (handler != null) handler(obj);
|
||||
get
|
||||
{
|
||||
return Path.Combine(configFolder, ConfigName + FileSuffix);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract string FileSuffix { get; }
|
||||
|
||||
protected abstract string ConfigName { get; }
|
||||
|
||||
private static object locker = new object();
|
||||
protected static T serializedObject;
|
||||
|
||||
public event Action<T> AfterLoad;
|
||||
|
||||
protected virtual void OnAfterLoad(T obj)
|
||||
{
|
||||
Action<T> handler = AfterLoad;
|
||||
if (handler != null) handler(obj);
|
||||
}
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (storage == null)
|
||||
if (serializedObject == null)
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
if (storage == null)
|
||||
if (serializedObject == null)
|
||||
{
|
||||
storage = new T();
|
||||
(storage as BaseStorage<T>).Load();
|
||||
serializedObject = new T();
|
||||
serializedObject.Load();
|
||||
}
|
||||
}
|
||||
}
|
||||
return storage;
|
||||
return serializedObject;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual T LoadDefaultConfig()
|
||||
/// <summary>
|
||||
/// if loading storage failed, we will try to load default
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected virtual T LoadDefault()
|
||||
{
|
||||
return storage;
|
||||
return serializedObject;
|
||||
}
|
||||
|
||||
private void Load()
|
||||
protected abstract void LoadInternal();
|
||||
protected abstract void SaveInternal();
|
||||
|
||||
public void Load()
|
||||
{
|
||||
string configPath = Path.Combine(configFolder, ConfigName + fileSuffix);
|
||||
if (!File.Exists(configPath))
|
||||
if (!File.Exists(ConfigPath))
|
||||
{
|
||||
if (!Directory.Exists(configFolder))
|
||||
{
|
||||
Directory.CreateDirectory(configFolder);
|
||||
File.Create(configPath).Close();
|
||||
}
|
||||
string json = File.ReadAllText(configPath);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
{
|
||||
try
|
||||
{
|
||||
storage = JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
storage = LoadDefaultConfig();
|
||||
}
|
||||
File.Create(ConfigPath).Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
storage = LoadDefaultConfig();
|
||||
}
|
||||
OnAfterLoadConfig(storage);
|
||||
LoadInternal();
|
||||
OnAfterLoad(serializedObject);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
//json is a good choise, readable and flexiable
|
||||
string configPath = Path.Combine(configFolder, ConfigName + fileSuffix);
|
||||
string json = JsonConvert.SerializeObject(storage, Formatting.Indented);
|
||||
File.WriteAllText(configPath, json);
|
||||
SaveInternal();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
45
Wox.Infrastructure/Storage/BinaryStorage.cs
Normal file
45
Wox.Infrastructure/Storage/BinaryStorage.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
/// <summary>
|
||||
/// Stroage object using binary data
|
||||
/// Normally, it has better performance, but not readable
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public abstract class BinaryStorage<T> : BaseStorage<T> where T : class, IStorage, new()
|
||||
{
|
||||
protected override string FileSuffix
|
||||
{
|
||||
get { return ".dat"; }
|
||||
}
|
||||
|
||||
protected override void LoadInternal()
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
||||
serializedObject = binaryFormatter.Deserialize(fileStream) as T;
|
||||
fileStream.Close();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
serializedObject = LoadDefault();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void SaveInternal()
|
||||
{
|
||||
FileStream fileStream = new FileStream(ConfigPath, FileMode.Create);
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
||||
binaryFormatter.Serialize(fileStream, serializedObject);
|
||||
fileStream.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Wox.Infrastructure/Storage/IStorage.cs
Normal file
13
Wox.Infrastructure/Storage/IStorage.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
public interface IStorage
|
||||
{
|
||||
void Load();
|
||||
void Save();
|
||||
}
|
||||
}
|
||||
46
Wox.Infrastructure/Storage/JsonStrorage.cs
Normal file
46
Wox.Infrastructure/Storage/JsonStrorage.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialize object using json format.
|
||||
/// </summary>
|
||||
public abstract class JsonStrorage<T> : BaseStorage<T> where T : class, IStorage, new()
|
||||
{
|
||||
protected override string FileSuffix
|
||||
{
|
||||
get { return ".json"; }
|
||||
}
|
||||
|
||||
protected override void LoadInternal()
|
||||
{
|
||||
string json = File.ReadAllText(ConfigPath);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
{
|
||||
try
|
||||
{
|
||||
serializedObject = JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
serializedObject = LoadDefault();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
serializedObject = LoadDefault();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void SaveInternal()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented);
|
||||
File.WriteAllText(ConfigPath, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ using Wox.Plugin;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
public class UserSelectedRecordStorage : BaseStorage<UserSelectedRecordStorage>
|
||||
public class UserSelectedRecordStorage : JsonStrorage<UserSelectedRecordStorage>
|
||||
{
|
||||
[JsonProperty]
|
||||
private Dictionary<string, int> records = new Dictionary<string, int>();
|
||||
|
||||
@@ -4,14 +4,16 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Wox.Infrastructure.Storage.UserSettings {
|
||||
public class FolderLink {
|
||||
namespace Wox.Infrastructure.Storage.UserSettings
|
||||
{
|
||||
public class FolderLink
|
||||
{
|
||||
[JsonProperty]
|
||||
public string Path { get; set; }
|
||||
public string Path { get; set; }
|
||||
|
||||
public string Nickname
|
||||
{
|
||||
get { return Path.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.None).Last(); }
|
||||
}
|
||||
public string Nickname
|
||||
{
|
||||
get { return Path.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.None).Last(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ using Newtonsoft.Json;
|
||||
|
||||
namespace Wox.Infrastructure.Storage.UserSettings
|
||||
{
|
||||
public class UserSettingStorage : BaseStorage<UserSettingStorage>
|
||||
public class UserSettingStorage : JsonStrorage<UserSettingStorage>
|
||||
{
|
||||
[JsonProperty]
|
||||
public bool DontPromptUpdateMsg { get; set; }
|
||||
@@ -146,7 +146,7 @@ namespace Wox.Infrastructure.Storage.UserSettings
|
||||
get { return "config"; }
|
||||
}
|
||||
|
||||
protected override UserSettingStorage LoadDefaultConfig()
|
||||
protected override UserSettingStorage LoadDefault()
|
||||
{
|
||||
DontPromptUpdateMsg = false;
|
||||
Theme = "Dark";
|
||||
@@ -165,7 +165,7 @@ namespace Wox.Infrastructure.Storage.UserSettings
|
||||
return this;
|
||||
}
|
||||
|
||||
protected override void OnAfterLoadConfig(UserSettingStorage storage)
|
||||
protected override void OnAfterLoad(UserSettingStorage storage)
|
||||
{
|
||||
if (storage.CustomizedPluginConfigs == null)
|
||||
{
|
||||
|
||||
26
Wox.Infrastructure/Timeit.cs
Normal file
26
Wox.Infrastructure/Timeit.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Infrastructure
|
||||
{
|
||||
public class Timeit : IDisposable
|
||||
{
|
||||
private Stopwatch stopwatch = new Stopwatch();
|
||||
private string name;
|
||||
|
||||
public Timeit(string name)
|
||||
{
|
||||
this.name = name;
|
||||
stopwatch.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
stopwatch.Stop();
|
||||
Debug.WriteLine(name + ":" + stopwatch.ElapsedMilliseconds + "ms");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,8 +58,12 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Logger\Log.cs" />
|
||||
<Compile Include="PeHeaderReader.cs" />
|
||||
<Compile Include="Storage\BinaryStorage.cs" />
|
||||
<Compile Include="Storage\IStorage.cs" />
|
||||
<Compile Include="Storage\JsonStrorage.cs" />
|
||||
<Compile Include="Storage\UserSettings\CustomizedPluginConfig.cs" />
|
||||
<Compile Include="Storage\UserSettings\FolderLink.cs" />
|
||||
<Compile Include="Timeit.cs" />
|
||||
<Compile Include="Unidecoder.Characters.cs" />
|
||||
<Compile Include="ChineseToPinYin.cs" />
|
||||
<Compile Include="HttpRequest.cs" />
|
||||
|
||||
Reference in New Issue
Block a user