|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
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>
|
|
|
|
|
|
|
|
public class JsonIOUtility
|
|
|
|
{
|
|
|
|
private string DefaultJsonFile = "";
|
|
|
|
private JsonIOCContainer jsonContainer = new JsonIOCContainer();
|
|
|
|
private JsonData mInstance = new JsonData();
|
|
|
|
public JsonIOUtility()
|
|
|
|
{
|
|
|
|
loadJsonFile();
|
|
|
|
}
|
|
|
|
public JsonIOUtility(string fileName)
|
|
|
|
{
|
|
|
|
loadJsonFile(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Setting FileName and Path(Default using Application.persistentDataPath)
|
|
|
|
private 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)
|
|
|
|
{
|
|
|
|
jsonContainer.Clear();
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
jsonContainer.Register(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Debug.LogWarning("No Such Data");
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#region DataTime
|
|
|
|
/// <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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IOCContainer
|
|
|
|
/// <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;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|