|
|
|
@ -1,38 +1,51 @@
@@ -1,38 +1,51 @@
|
|
|
|
|
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 |
|
|
|
|
{ |
|
|
|
|
/// <summary> |
|
|
|
|
/// how to use: |
|
|
|
|
/// JsonIOUtitlty jsonIO = new JsonIOUtility(); or JsonIOUtitlty jsonIO = new JsonIOUtility("myJsonFile"); |
|
|
|
|
/// //Save data to native json file; |
|
|
|
|
/// jsonIO.setData(TypeClass object); |
|
|
|
|
/// jsonIO.SaveToJsonFile(); |
|
|
|
|
/// //Read data to native json file; |
|
|
|
|
/// jsonIO.getData<TypeClass>(); |
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace JXSoft { |
|
|
|
|
public class JsonIOUtility |
|
|
|
|
public class JsonIOUtility |
|
|
|
|
{ |
|
|
|
|
private string DefaultJsonFile = ""; |
|
|
|
|
private JsonIOCContainer jsonContainer = new JsonIOCContainer(); |
|
|
|
|
private JsonData mInstance = new JsonData(); |
|
|
|
|
public JsonIOUtility() { |
|
|
|
|
public JsonIOUtility() |
|
|
|
|
{ |
|
|
|
|
loadJsonFile(); |
|
|
|
|
} |
|
|
|
|
public JsonIOUtility(string fileName) |
|
|
|
|
{ |
|
|
|
|
loadJsonFile(fileName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//Setting FileName and Path(Default using Application.persistentDataPath) |
|
|
|
|
public void setLoadFile(string fileName) |
|
|
|
|
private void setLoadFile(string fileName) |
|
|
|
|
{ |
|
|
|
|
DefaultJsonFile = Application.persistentDataPath +"/" + fileName + ".json"; |
|
|
|
|
DefaultJsonFile = Application.persistentDataPath + "/" + fileName + ".json"; |
|
|
|
|
Debug.Log(DefaultJsonFile); |
|
|
|
|
} |
|
|
|
|
#region load |
|
|
|
|
//Load From File |
|
|
|
|
public void loadJsonFile() { |
|
|
|
|
public void loadJsonFile() |
|
|
|
|
{ |
|
|
|
|
loadJsonFile("JsonConfig"); |
|
|
|
|
} |
|
|
|
|
public void loadJsonFile(string fileName) |
|
|
|
|
{ |
|
|
|
|
jsonContainer.Clear(); |
|
|
|
|
setLoadFile(fileName); |
|
|
|
|
if (!File.Exists(DefaultJsonFile)) |
|
|
|
|
{ |
|
|
|
@ -48,9 +61,11 @@ namespace JXSoft {
@@ -48,9 +61,11 @@ namespace JXSoft {
|
|
|
|
|
|
|
|
|
|
#region save |
|
|
|
|
//Save to local |
|
|
|
|
public void SaveToJsonFile() { |
|
|
|
|
public void SaveToJsonFile() |
|
|
|
|
{ |
|
|
|
|
setData(new JsonIOUtilitySaveDate()); |
|
|
|
|
foreach (Type type in jsonContainer.getInstance().Keys) { |
|
|
|
|
foreach (Type type in jsonContainer.getInstance().Keys) |
|
|
|
|
{ |
|
|
|
|
string data = JsonMapper.ToJson(jsonContainer.getInstance()[type]); |
|
|
|
|
mInstance[type.ToString()] = JsonMapper.ToObject(data); |
|
|
|
|
} |
|
|
|
@ -64,28 +79,45 @@ namespace JXSoft {
@@ -64,28 +79,45 @@ namespace JXSoft {
|
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
//Loading FileList From Folder(Default using Application.persistentDataPath) |
|
|
|
|
public void loadFileList() { |
|
|
|
|
|
|
|
|
|
#region fileListOpreate |
|
|
|
|
private DataDirectory dataDirectory; |
|
|
|
|
//Loading FileList From Folder(Default using Application.persistentDataPath) |
|
|
|
|
public void loadFileList() |
|
|
|
|
{ |
|
|
|
|
GetDataDirectoryQuery getDirectory = new GetDataDirectoryQuery(Application.persistentDataPath); |
|
|
|
|
dataDirectory = getDirectory.DoQuery(); |
|
|
|
|
} |
|
|
|
|
public void loadFileList(string path) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(Application.persistentDataPath + "/" + path)) |
|
|
|
|
{ |
|
|
|
|
Directory.CreateDirectory(Application.persistentDataPath + "/" + path); |
|
|
|
|
} |
|
|
|
|
GetDataDirectoryQuery getDirectory = new GetDataDirectoryQuery(Application.persistentDataPath, path); |
|
|
|
|
dataDirectory = getDirectory.DoQuery(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public DataDirectory GetDataDirectory() |
|
|
|
|
{ |
|
|
|
|
return dataDirectory; |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region Save&Read interface |
|
|
|
|
public void setData<TData>(TData data) where TData : class { |
|
|
|
|
public void setData<TData>(TData data) where TData : class
|
|
|
|
|
{ |
|
|
|
|
jsonContainer.Register(data); |
|
|
|
|
} |
|
|
|
|
public TData getData<TData>() where TData : class,new() |
|
|
|
|
public TData getData<TData>() where TData : class, new() |
|
|
|
|
{ |
|
|
|
|
TData data = jsonContainer.Get<TData>(); |
|
|
|
|
if (data == null) { |
|
|
|
|
if (data == null) |
|
|
|
|
{ |
|
|
|
|
var key = typeof(TData); |
|
|
|
|
if (mInstance.ContainsKey(key.ToString())) |
|
|
|
|
{ |
|
|
|
|
data = JsonMapper.ToObject<TData>(mInstance[key.ToString()].ToJson()); |
|
|
|
|
jsonContainer.Register(data); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
Debug.LogWarning("No Such Data"); |
|
|
|
@ -96,6 +128,7 @@ namespace JXSoft {
@@ -96,6 +128,7 @@ namespace JXSoft {
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#region DataTime |
|
|
|
|
/// <summary> |
|
|
|
|
/// Every Time Create&Save Recording this Time |
|
|
|
|
/// </summary> |
|
|
|
@ -103,7 +136,8 @@ namespace JXSoft {
@@ -103,7 +136,8 @@ namespace JXSoft {
|
|
|
|
|
{ |
|
|
|
|
public long timeStamp; |
|
|
|
|
public string timeStr; |
|
|
|
|
public JsonIOUtilitySaveDate() { |
|
|
|
|
public JsonIOUtilitySaveDate() |
|
|
|
|
{ |
|
|
|
|
timeStamp = DateUtil.getTimeStamp(); |
|
|
|
|
timeStr = DateUtil.getFullDateMsg(); |
|
|
|
|
} |
|
|
|
@ -125,7 +159,9 @@ namespace JXSoft {
@@ -125,7 +159,9 @@ namespace JXSoft {
|
|
|
|
|
return Convert.ToInt64(ts.TotalSeconds); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region IOCContainer |
|
|
|
|
/// <summary> |
|
|
|
|
/// IOC For Json |
|
|
|
|
/// </summary> |
|
|
|
@ -158,9 +194,148 @@ namespace JXSoft {
@@ -158,9 +194,148 @@ namespace JXSoft {
|
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
public Dictionary<Type, object> getInstance() { |
|
|
|
|
public Dictionary<Type, object> getInstance() |
|
|
|
|
{ |
|
|
|
|
return mInstances; |
|
|
|
|
} |
|
|
|
|
public void Clear() |
|
|
|
|
{ |
|
|
|
|
mInstances.Clear(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//存放当前目录信息(包括文件夹以及Json存档,如果以后相加新模块在结构类里添加新的list即可) |
|
|
|
|
public class DataDirectory |
|
|
|
|
{ |
|
|
|
|
//当前的目录 |
|
|
|
|
public string directoryName; |
|
|
|
|
public string absPathName = ""; |
|
|
|
|
//如果有父级 |
|
|
|
|
private string parentsPathName; |
|
|
|
|
//当前目录包含的文件夹名称 |
|
|
|
|
public List<string> childDirectoryNames; |
|
|
|
|
//当前目录包含的JsonFile |
|
|
|
|
public List<string> childJsonFile; |
|
|
|
|
public DataDirectory(string directoryName, List<string> childDirectoryNames, List<string> childVideoNames) |
|
|
|
|
{ |
|
|
|
|
this.directoryName = directoryName; |
|
|
|
|
this.childDirectoryNames = childDirectoryNames; |
|
|
|
|
this.childJsonFile = childVideoNames; |
|
|
|
|
this.absPathName = ""; |
|
|
|
|
} |
|
|
|
|
public DataDirectory(string directoryName, string absPath, List<string> childDirectoryNames, List<string> childJsonFile) |
|
|
|
|
{ |
|
|
|
|
this.directoryName = directoryName; |
|
|
|
|
this.childDirectoryNames = childDirectoryNames; |
|
|
|
|
this.childJsonFile = childJsonFile; |
|
|
|
|
this.absPathName = absPath; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//测试用,结构化输出 |
|
|
|
|
public string toString() |
|
|
|
|
{ |
|
|
|
|
string msg = ""; |
|
|
|
|
msg += "Path:\n" + directoryName + "\n"; |
|
|
|
|
msg += "AbsPath:\n" + absPathName + "\n"; |
|
|
|
|
msg += "ParentPath:\n" + getParentPath() + "\n"; |
|
|
|
|
msg += "Directroy:\n"; |
|
|
|
|
foreach (string s in childDirectoryNames) |
|
|
|
|
{ |
|
|
|
|
msg = msg + s + "|"; |
|
|
|
|
} |
|
|
|
|
msg += "\nJsonFile:\n"; |
|
|
|
|
foreach (string s in childJsonFile) |
|
|
|
|
{ |
|
|
|
|
msg = msg + s + "|"; |
|
|
|
|
} |
|
|
|
|
return msg; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public string getAbsPath() |
|
|
|
|
{ |
|
|
|
|
return absPathName + "/"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public string getParentPath() |
|
|
|
|
{ |
|
|
|
|
parentsPathName = getParentPathWithPath(directoryName); |
|
|
|
|
return parentsPathName; |
|
|
|
|
} |
|
|
|
|
public string getAbsParentPath() |
|
|
|
|
{ |
|
|
|
|
//存在多级目录的 |
|
|
|
|
return getParentPathWithPath(absPathName); |
|
|
|
|
} |
|
|
|
|
private string getParentPathWithPath(string originPath) |
|
|
|
|
{ |
|
|
|
|
string parentsPath_ = ""; |
|
|
|
|
string[] dirArray = originPath.Split('/'); |
|
|
|
|
dirArray[dirArray.Length - 1] = ""; |
|
|
|
|
foreach (string dir in dirArray) |
|
|
|
|
{ |
|
|
|
|
if (!"".Equals(parentsPath_) && !"".Equals(dir)) |
|
|
|
|
{ |
|
|
|
|
parentsPath_ += "/"; |
|
|
|
|
} |
|
|
|
|
parentsPath_ += dir; |
|
|
|
|
} |
|
|
|
|
if (parentsPath_.Equals("/")) |
|
|
|
|
{ |
|
|
|
|
parentsPath_ = ""; |
|
|
|
|
} |
|
|
|
|
return parentsPath_; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#region Query |
|
|
|
|
public class GetDataDirectoryQuery |
|
|
|
|
{ |
|
|
|
|
//当前目录名称 |
|
|
|
|
private string directoryName; |
|
|
|
|
private string absPathName = ""; |
|
|
|
|
public GetDataDirectoryQuery(string directoryName) |
|
|
|
|
{ |
|
|
|
|
this.directoryName = directoryName; |
|
|
|
|
} |
|
|
|
|
public GetDataDirectoryQuery(string directoryName, string absPathName) |
|
|
|
|
{ |
|
|
|
|
this.directoryName = directoryName + "/" + absPathName; |
|
|
|
|
this.absPathName = absPathName; |
|
|
|
|
} |
|
|
|
|
public DataDirectory DoQuery() |
|
|
|
|
{ |
|
|
|
|
List<string> childDirectoryNames = new List<string>(); |
|
|
|
|
List<string> childJsonFileNames = new List<string>(); |
|
|
|
|
DirectoryInfo dirInfo = new DirectoryInfo(directoryName); |
|
|
|
|
FileInfo[] fileInfos = dirInfo.GetFiles(); |
|
|
|
|
DirectoryInfo[] directroyInfos = dirInfo.GetDirectories(); |
|
|
|
|
//------------------------getDirectoryNames----------------// |
|
|
|
|
foreach (DirectoryInfo di in directroyInfos) |
|
|
|
|
{ |
|
|
|
|
childDirectoryNames.Add(di.Name); |
|
|
|
|
} |
|
|
|
|
//------------------------getVideoNames----------------// |
|
|
|
|
for (int i = 0; i < fileInfos.Length; i++) |
|
|
|
|
{ |
|
|
|
|
//有后缀的名字 |
|
|
|
|
string fileNameWithSuffix = fileInfos[i].Name; |
|
|
|
|
int index = fileNameWithSuffix.LastIndexOf('.'); |
|
|
|
|
//如果后缀不是json直接下一个 |
|
|
|
|
if (fileNameWithSuffix.Length - index <= 4) |
|
|
|
|
{ |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if (fileNameWithSuffix.Substring(index + 1, 4) != "json") continue; |
|
|
|
|
//没有后缀的名字 |
|
|
|
|
string fileName = fileNameWithSuffix.Substring(0, index); |
|
|
|
|
childJsonFileNames.Add(fileName); |
|
|
|
|
} |
|
|
|
|
DataDirectory directories = new DataDirectory(this.directoryName, this.absPathName, childDirectoryNames, childJsonFileNames); |
|
|
|
|
return directories; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|