You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
4.7 KiB
164 lines
4.7 KiB
using System; |
|
using System.IO; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using System.Configuration; |
|
using System.Linq; |
|
using LitJson; |
|
using System.Text; |
|
|
|
namespace JXSoft { |
|
public class JsonIOUtility |
|
{ |
|
private string DefaultJsonFile = ""; |
|
private JsonIOCContainer jsonContainer = new JsonIOCContainer(); |
|
private JsonData mInstance = new JsonData(); |
|
public JsonIOUtility() { |
|
loadJsonFile(); |
|
} |
|
|
|
//Setting FileName and Path(Default using Application.persistentDataPath) |
|
public void setLoadFile(string fileName) |
|
{ |
|
DefaultJsonFile = Application.persistentDataPath +"/" + fileName + ".json"; |
|
Debug.Log(DefaultJsonFile); |
|
} |
|
#region load |
|
//Load From File |
|
public void loadJsonFile() { |
|
loadJsonFile("JsonConfig"); |
|
} |
|
public void loadJsonFile(string fileName) |
|
{ |
|
setLoadFile(fileName); |
|
if (!File.Exists(DefaultJsonFile)) |
|
{ |
|
Debug.LogWarning("No Such File,create New"); |
|
File.Create(DefaultJsonFile).Dispose(); |
|
SaveToJsonFile(); |
|
//return null; |
|
} |
|
string jsonFile = File.ReadAllText(DefaultJsonFile); |
|
mInstance = JsonMapper.ToObject(jsonFile); |
|
} |
|
#endregion |
|
|
|
#region save |
|
//Save to local |
|
public void SaveToJsonFile() { |
|
setData(new JsonIOUtilitySaveDate()); |
|
foreach (Type type in jsonContainer.getInstance().Keys) { |
|
string data = JsonMapper.ToJson(jsonContainer.getInstance()[type]); |
|
mInstance[type.ToString()] = JsonMapper.ToObject(data); |
|
} |
|
StringBuilder sb = new StringBuilder(); |
|
JsonWriter jWriter = new JsonWriter(sb); |
|
jWriter.PrettyPrint = true; |
|
jWriter.IndentValue = 4; |
|
JsonMapper.ToJson(mInstance, jWriter); |
|
Debug.Log("saveFileData:" + mInstance.ToJson()); |
|
File.WriteAllText(DefaultJsonFile, sb.ToString()); |
|
} |
|
#endregion |
|
|
|
//Loading FileList From Folder(Default using Application.persistentDataPath) |
|
public void loadFileList() { |
|
|
|
} |
|
public void loadFileList(string path) |
|
{ |
|
|
|
} |
|
|
|
|
|
#region Save&Read interface |
|
public void setData<TData>(TData data) where TData : class { |
|
jsonContainer.Register(data); |
|
} |
|
public TData getData<TData>() where TData : class,new() |
|
{ |
|
TData data = jsonContainer.Get<TData>(); |
|
if (data == null) { |
|
var key = typeof(TData); |
|
if (mInstance.ContainsKey(key.ToString())) |
|
{ |
|
data = JsonMapper.ToObject<TData>(mInstance[key.ToString()].ToJson()); |
|
} |
|
else |
|
Debug.LogWarning("No Such Data"); |
|
} |
|
return data; |
|
} |
|
#endregion |
|
|
|
} |
|
|
|
/// <summary> |
|
/// Every Time Create&Save Recording this Time |
|
/// </summary> |
|
public class JsonIOUtilitySaveDate |
|
{ |
|
public long timeStamp; |
|
public string timeStr; |
|
public JsonIOUtilitySaveDate() { |
|
timeStamp = DateUtil.getTimeStamp(); |
|
timeStr = DateUtil.getFullDateMsg(); |
|
} |
|
} |
|
|
|
public class DateUtil |
|
{ |
|
public static string getFullDateMsg() |
|
{ |
|
return DateTime.Now.ToString("yyyy-MM-dd") + " " + DateTime.Now.ToString("HH:mm:ss"); |
|
} |
|
public static string getDayDateMsg() |
|
{ |
|
return DateTime.Now.ToString("HH:mm:ss"); |
|
} |
|
public static long getTimeStamp() |
|
{ |
|
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0); |
|
return Convert.ToInt64(ts.TotalSeconds); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// IOC For Json |
|
/// </summary> |
|
public class JsonIOCContainer |
|
{ |
|
private Dictionary<Type, object> mInstances = new Dictionary<Type, object>(); |
|
|
|
public void Register<T>(T instance) |
|
{ |
|
var key = typeof(T); |
|
|
|
if (mInstances.ContainsKey(key)) |
|
{ |
|
mInstances[key] = instance; |
|
} |
|
else |
|
{ |
|
mInstances.Add(key, instance); |
|
} |
|
} |
|
|
|
public T Get<T>() where T : class |
|
{ |
|
var key = typeof(T); |
|
|
|
if (mInstances.TryGetValue(key, out var retInstance)) |
|
{ |
|
return retInstance as T; |
|
} |
|
|
|
return null; |
|
} |
|
public Dictionary<Type, object> getInstance() { |
|
return mInstances; |
|
} |
|
} |
|
} |
|
|
|
|