2014-03-23 16:17:41 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace Wox.Infrastructure.Storage
|
|
|
|
|
{
|
|
|
|
|
public abstract class BaseStorage<T> where T : class, 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;
|
|
|
|
|
|
2014-07-01 22:19:46 +08:00
|
|
|
|
public event Action<T> AfterLoadConfig;
|
|
|
|
|
|
|
|
|
|
protected virtual void OnAfterLoadConfig(T obj)
|
|
|
|
|
{
|
|
|
|
|
Action<T> handler = AfterLoadConfig;
|
|
|
|
|
if (handler != null) handler(obj);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 16:17:41 +08:00
|
|
|
|
protected abstract string ConfigName { get; }
|
|
|
|
|
|
|
|
|
|
public static T Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (storage == null)
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
if (storage == null)
|
|
|
|
|
{
|
|
|
|
|
storage = new T();
|
|
|
|
|
(storage as BaseStorage<T>).Load();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return storage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-17 15:57:25 +08:00
|
|
|
|
protected virtual T LoadDefaultConfig()
|
|
|
|
|
{
|
|
|
|
|
return storage;
|
|
|
|
|
}
|
2014-03-23 18:14:46 +08:00
|
|
|
|
|
2014-03-23 16:17:41 +08:00
|
|
|
|
private void Load()
|
|
|
|
|
{
|
|
|
|
|
string configPath = Path.Combine(configFolder, ConfigName + fileSuffix);
|
|
|
|
|
if (!File.Exists(configPath))
|
|
|
|
|
{
|
2014-03-23 16:58:19 +08:00
|
|
|
|
if (!Directory.Exists(configFolder))
|
|
|
|
|
Directory.CreateDirectory(configFolder);
|
2014-03-23 16:17:41 +08:00
|
|
|
|
File.Create(configPath).Close();
|
|
|
|
|
}
|
|
|
|
|
string json = File.ReadAllText(configPath);
|
|
|
|
|
if (!string.IsNullOrEmpty(json))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
storage = JsonConvert.DeserializeObject<T>(json);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2014-08-17 15:57:25 +08:00
|
|
|
|
storage = LoadDefaultConfig();
|
2014-03-23 16:17:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-23 18:14:46 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2014-08-17 15:57:25 +08:00
|
|
|
|
storage = LoadDefaultConfig();
|
2014-03-23 18:14:46 +08:00
|
|
|
|
}
|
2014-08-17 15:57:25 +08:00
|
|
|
|
OnAfterLoadConfig(storage);
|
2014-03-23 16:17:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|