2014-03-23 16:17:41 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using System.Reflection;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
using System.Runtime.InteropServices.ComTypes;
|
2014-03-23 16:17:41 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace Wox.Infrastructure.Storage
|
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
[Serializable]
|
2014-12-29 21:55:27 +08:00
|
|
|
|
public abstract class BaseStorage<T> : IStorage where T : class,IStorage, new()
|
2014-03-23 16:17:41 +08:00
|
|
|
|
{
|
2015-01-05 22:41:17 +08:00
|
|
|
|
protected abstract string ConfigFolder { get; }
|
2014-12-15 22:58:49 +08:00
|
|
|
|
|
|
|
|
|
protected string ConfigPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-12-29 21:55:27 +08:00
|
|
|
|
return Path.Combine(ConfigFolder, ConfigName + FileSuffix);
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-29 21:55:27 +08:00
|
|
|
|
|
2014-12-15 22:58:49 +08:00
|
|
|
|
protected abstract string FileSuffix { get; }
|
|
|
|
|
|
|
|
|
|
protected abstract string ConfigName { get; }
|
|
|
|
|
|
2014-03-23 16:17:41 +08:00
|
|
|
|
private static object locker = new object();
|
2014-12-15 22:58:49 +08:00
|
|
|
|
protected static T serializedObject;
|
2014-03-23 16:17:41 +08:00
|
|
|
|
|
2014-12-15 22:58:49 +08:00
|
|
|
|
public event Action<T> AfterLoad;
|
2014-07-01 22:19:46 +08:00
|
|
|
|
|
2014-12-15 22:58:49 +08:00
|
|
|
|
protected virtual void OnAfterLoad(T obj)
|
2014-07-01 22:19:46 +08:00
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
Action<T> handler = AfterLoad;
|
2014-07-01 22:19:46 +08:00
|
|
|
|
if (handler != null) handler(obj);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 16:17:41 +08:00
|
|
|
|
public static T Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
if (serializedObject == null)
|
2014-03-23 16:17:41 +08:00
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
if (serializedObject == null)
|
2014-03-23 16:17:41 +08:00
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
serializedObject = new T();
|
|
|
|
|
serializedObject.Load();
|
2014-03-23 16:17:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-15 22:58:49 +08:00
|
|
|
|
return serializedObject;
|
2014-03-23 16:17:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-15 22:58:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// if loading storage failed, we will try to load default
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected virtual T LoadDefault()
|
2014-08-17 15:57:25 +08:00
|
|
|
|
{
|
2015-01-04 18:14:50 +08:00
|
|
|
|
return new T();
|
2014-08-17 15:57:25 +08:00
|
|
|
|
}
|
2014-03-23 18:14:46 +08:00
|
|
|
|
|
2014-12-15 22:58:49 +08:00
|
|
|
|
protected abstract void LoadInternal();
|
|
|
|
|
protected abstract void SaveInternal();
|
|
|
|
|
|
|
|
|
|
public void Load()
|
2014-03-23 16:17:41 +08:00
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
if (!File.Exists(ConfigPath))
|
2014-03-23 16:17:41 +08:00
|
|
|
|
{
|
2014-12-29 21:55:27 +08:00
|
|
|
|
if (!Directory.Exists(ConfigFolder))
|
2014-03-23 16:17:41 +08:00
|
|
|
|
{
|
2014-12-29 21:55:27 +08:00
|
|
|
|
Directory.CreateDirectory(ConfigFolder);
|
2014-03-23 16:17:41 +08:00
|
|
|
|
}
|
2014-12-15 22:58:49 +08:00
|
|
|
|
File.Create(ConfigPath).Close();
|
2014-03-23 16:17:41 +08:00
|
|
|
|
}
|
2014-12-15 22:58:49 +08:00
|
|
|
|
LoadInternal();
|
|
|
|
|
OnAfterLoad(serializedObject);
|
2014-03-23 16:17:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
SaveInternal();
|
2014-03-23 16:17:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|