2014-03-23 16:17:41 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace Wox.Infrastructure.Storage
|
|
|
|
|
{
|
2014-12-15 22:58:49 +08:00
|
|
|
|
[Serializable]
|
2016-01-07 10:31:17 +08:00
|
|
|
|
public abstract class BaseStorage<T> : IStorage where T : class, IStorage, new()
|
2014-03-23 16:17:41 +08:00
|
|
|
|
{
|
2016-01-07 10:31:17 +08:00
|
|
|
|
protected string DirectoryPath { get; } = Path.Combine(WoxDirectroy.Executable, "Config");
|
2014-12-15 22:58:49 +08:00
|
|
|
|
|
2016-01-07 10:31:17 +08:00
|
|
|
|
protected string FilePath => Path.Combine(DirectoryPath, FileName + FileSuffix);
|
2014-12-29 21:55:27 +08:00
|
|
|
|
|
2014-12-15 22:58:49 +08:00
|
|
|
|
protected abstract string FileSuffix { get; }
|
|
|
|
|
|
2016-01-07 10:31:17 +08:00
|
|
|
|
protected abstract string FileName { get; }
|
2014-12-15 22:58:49 +08:00
|
|
|
|
|
2014-03-23 16:17:41 +08:00
|
|
|
|
private static object locker = new object();
|
2016-01-07 10:31:17 +08:00
|
|
|
|
|
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
|
|
|
|
{
|
2016-01-07 10:31:17 +08:00
|
|
|
|
if (!File.Exists(FilePath))
|
2014-03-23 16:17:41 +08:00
|
|
|
|
{
|
2016-01-07 10:31:17 +08:00
|
|
|
|
if (!Directory.Exists(DirectoryPath))
|
2014-03-23 16:17:41 +08:00
|
|
|
|
{
|
2016-01-07 10:31:17 +08:00
|
|
|
|
Directory.CreateDirectory(DirectoryPath);
|
2014-03-23 16:17:41 +08:00
|
|
|
|
}
|
2016-01-07 10:31:17 +08:00
|
|
|
|
File.Create(FilePath).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
|
|
|
|
}
|