162 lines
4.3 KiB
C#
162 lines
4.3 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace CeramicProjectTool.Util
|
|||
|
{
|
|||
|
public static class Store
|
|||
|
{
|
|||
|
private static string _storePath = PathUtil.AppRoot;
|
|||
|
private static Dictionary<string, object> datas = new Dictionary<string, object>();
|
|||
|
public static void StoreData(string key, string value)
|
|||
|
{
|
|||
|
// Store data to local file
|
|||
|
}
|
|||
|
|
|||
|
public static bool SaveOrUpdateDataToFile(string key, object value)
|
|||
|
{
|
|||
|
if (datas.ContainsKey(key))
|
|||
|
{
|
|||
|
datas[key] = value;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
datas.Add(key, value);
|
|||
|
}
|
|||
|
return SaveToFile(key);
|
|||
|
}
|
|||
|
|
|||
|
public static T? GetStoreData<T>(string key)
|
|||
|
{
|
|||
|
if (datas.ContainsKey(key))
|
|||
|
{
|
|||
|
var data = datas[key];
|
|||
|
if (!(data is T))
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var dataJson = Serialize(data);
|
|||
|
var dataObj = Deserialize<T>(dataJson);
|
|||
|
return dataObj;
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
|
|||
|
throw new InvalidCastException("数据类型不一致!");
|
|||
|
}
|
|||
|
}
|
|||
|
return (T)data;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (LoadFromFile(key))
|
|||
|
{
|
|||
|
return GetStoreData<T>(key);
|
|||
|
}
|
|||
|
return default(T);
|
|||
|
}
|
|||
|
}
|
|||
|
public static void Init()
|
|||
|
{
|
|||
|
if (_storePath == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
// Load data from local file
|
|||
|
loadAll();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static bool SaveAll()
|
|||
|
{
|
|||
|
foreach (var key in datas.Keys)
|
|||
|
{
|
|||
|
if (!SaveToFile(key))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public static bool SaveToFile(string key)
|
|||
|
{
|
|||
|
var filePath = GetFilePath(key);
|
|||
|
var data = Serialize(datas[key]);
|
|||
|
try
|
|||
|
{
|
|||
|
System.IO.File.WriteAllText(filePath, data);
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Console.WriteLine(ex);
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
public static bool LoadFromFile(string key)
|
|||
|
{
|
|||
|
var filePath = GetFilePath(key);
|
|||
|
if (!System.IO.File.Exists(filePath))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
try
|
|||
|
{
|
|||
|
var data = System.IO.File.ReadAllText(filePath);
|
|||
|
datas[key] = Deserialize<object>(data);
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Console.WriteLine(ex);
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static string GetFilePath(string key)
|
|||
|
{
|
|||
|
var fullPath = _storePath;
|
|||
|
if (!System.IO.Directory.Exists(fullPath))
|
|||
|
{
|
|||
|
System.IO.Directory.CreateDirectory(fullPath);
|
|||
|
}
|
|||
|
|
|||
|
return System.IO.Path.Combine(fullPath, key);
|
|||
|
}
|
|||
|
public static bool loadAll()
|
|||
|
{
|
|||
|
//var fullPath = System.IO.Path.Combine(AppRoot, FILE_STORE_PATH);
|
|||
|
if (!System.IO.Directory.Exists(_storePath))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
var files = System.IO.Directory.GetFiles(_storePath);
|
|||
|
foreach (var file in files)
|
|||
|
{
|
|||
|
var key = System.IO.Path.GetFileName(file);
|
|||
|
key = key.Replace(".json", "");
|
|||
|
if (!LoadFromFile(key))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
#region 序列化
|
|||
|
private static string Serialize(object obj)
|
|||
|
{
|
|||
|
return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
|
|||
|
}
|
|||
|
private static T? Deserialize<T>(string json)
|
|||
|
{
|
|||
|
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
}
|
|||
|
}
|