jkpete
2 months ago
14 changed files with 190 additions and 124 deletions
@ -0,0 +1,67 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using Newtonsoft.Json; |
||||||
|
using Newtonsoft.Json.Linq; |
||||||
|
|
||||||
|
namespace EGFramework |
||||||
|
{ |
||||||
|
public class EGJsonSave : IEGSave,IEGSaveObject |
||||||
|
{ |
||||||
|
private string DefaultPath { set; get; } |
||||||
|
private JObject _SaveObject; |
||||||
|
private JObject SaveObject{ |
||||||
|
get { |
||||||
|
if(_SaveObject == null){ |
||||||
|
InitSaveFile(DefaultPath); |
||||||
|
} |
||||||
|
return _SaveObject; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Init a new save data file or load an other file with json suffix, if you want to load other save data, please use this function to reload; |
||||||
|
/// </summary> |
||||||
|
public void InitSaveFile(string path) |
||||||
|
{ |
||||||
|
DefaultPath = path; |
||||||
|
if(!File.Exists(path)){ |
||||||
|
if (!Directory.Exists(DefaultPath)) |
||||||
|
{ |
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath)); |
||||||
|
File.WriteAllText(DefaultPath,"{}"); |
||||||
|
}else if(!File.Exists(DefaultPath)){ |
||||||
|
File.WriteAllText(DefaultPath,"{}"); |
||||||
|
} |
||||||
|
} |
||||||
|
using (StreamReader reader = File.OpenText(path)) |
||||||
|
{ |
||||||
|
_SaveObject = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void SetObject<TObject>(string objectKey,TObject obj) |
||||||
|
{ |
||||||
|
if(SaveObject.ContainsKey(typeof(TObject).ToString())){ |
||||||
|
SaveObject[typeof(TObject).ToString()] = JToken.FromObject(obj); |
||||||
|
}else{ |
||||||
|
SaveObject.Add(typeof(TObject).ToString(),JToken.FromObject(obj)); |
||||||
|
} |
||||||
|
File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented)); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Get data from file, if your data is not in file, then throw an exception. |
||||||
|
/// </summary> |
||||||
|
public TObject GetObject<TObject>(string objectKey) where TObject : new() |
||||||
|
{ |
||||||
|
if(!SaveObject.ContainsKey(typeof(TObject).ToString())){ |
||||||
|
throw new Exception("Key not found!"); |
||||||
|
} |
||||||
|
TObject data = SaveObject[typeof(TObject).ToString()].ToObject<TObject>(); |
||||||
|
return data; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,131 +1,103 @@ |
|||||||
using System; |
using System; |
||||||
using System.IO; |
using System.IO; |
||||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||||
using Newtonsoft.Json; |
using Godot; |
||||||
using Newtonsoft.Json.Bson; |
|
||||||
using Newtonsoft.Json.Linq; |
|
||||||
|
|
||||||
namespace EGFramework |
namespace EGFramework |
||||||
{ |
{ |
||||||
public interface IEGSave{ |
|
||||||
void SetDataToFile<TData>(TData data); |
|
||||||
TData GetDataByFile<TData>() where TData : class,new(); |
|
||||||
void InitSaveData(string path); |
|
||||||
} |
|
||||||
public enum TypeEGSave{ |
public enum TypeEGSave{ |
||||||
Json = 0, |
Json = 0, |
||||||
Bson = 1, |
Bson = 1, |
||||||
Byte = 2, |
Byte = 2, |
||||||
Sqlite = 3, |
XML = 3 |
||||||
LiteDB = 4, |
} |
||||||
XML = 5 |
public enum TypeDBSave{ |
||||||
|
Csv = 0, |
||||||
|
Sqlite = 1, |
||||||
|
LiteDB = 2, |
||||||
} |
} |
||||||
|
|
||||||
public class EGSave : EGModule,IEGSave |
public class EGSave : EGModule |
||||||
{ |
{ |
||||||
private string DefaultPath = "Default/SaveData.json"; |
#region About Godot File's PATH |
||||||
private JObject _SaveObject; |
// Godot's Path has res:// and user:// |
||||||
private JObject SaveObject{ |
// UserPath is used for every platform such as android. |
||||||
get { |
// You can use ProjectSettings.GlobalizePath("") to convert a "local" path like res://path/to/file.txt to an absolute OS path. |
||||||
if(_SaveObject == null){ |
#endregion |
||||||
InitSaveObject(); |
|
||||||
} |
private Dictionary<string,IEGSaveData> DataBaseFiles = new Dictionary<string,IEGSaveData>(); |
||||||
return _SaveObject; |
private Dictionary<string,IEGSaveObject> ObjectFiles = new Dictionary<string,IEGSaveObject>(); |
||||||
} |
|
||||||
} |
|
||||||
public EGSave() {} |
public EGSave() {} |
||||||
public override void Init() |
public override void Init() |
||||||
{ |
{ |
||||||
if (!Directory.Exists(DefaultPath)) |
LoadObjectFile("Default/SaveData.json".GetGodotUserPath(),TypeEGSave.Json); |
||||||
{ |
} |
||||||
Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath)); |
|
||||||
File.WriteAllText(DefaultPath,"{}"); |
public void LoadDataFile(string path,TypeDBSave type){ |
||||||
}else if(!File.Exists(DefaultPath)){ |
switch(type){ |
||||||
File.WriteAllText(DefaultPath,"{}"); |
case TypeDBSave.Csv: |
||||||
|
break; |
||||||
|
default: |
||||||
|
break; |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
private void InitSaveObject(){ |
public void LoadObjectFile(string path,TypeEGSave type){ |
||||||
using (StreamReader reader = File.OpenText(DefaultPath)) |
switch(type){ |
||||||
{ |
case TypeEGSave.Json: |
||||||
_SaveObject = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); |
EGJsonSave newJsonFile = new EGJsonSave(); |
||||||
|
newJsonFile.InitSaveFile(path); |
||||||
|
ObjectFiles.Add(path, newJsonFile); |
||||||
|
break; |
||||||
|
default: |
||||||
|
break; |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
/// <summary> |
public void SetObject<TObject>(string path,string objectKey,TObject obj){ |
||||||
/// Push SaveObject data set to file |
ObjectFiles[path].SetObject(objectKey,obj); |
||||||
/// </summary> |
|
||||||
public void SaveToFile(){ |
|
||||||
SaveToFile(DefaultPath); |
|
||||||
} |
} |
||||||
private void SaveToFile(string fileName){ |
public TObject GetObject<TObject>(string path,string key) where TObject : new(){ |
||||||
File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented)); |
return ObjectFiles[path].GetObject<TObject>(key); |
||||||
} |
} |
||||||
|
|
||||||
/// <summary> |
//------------------------------------------------------------------------------// |
||||||
/// Push data to SaveObject object cache, this function will not save data to file, if you hope not to IO operation frequently, you can use this with SaveToFile. |
|
||||||
/// </summary> |
|
||||||
/// <param name="data"></param> |
|
||||||
/// <typeparam name="TData"></typeparam> |
|
||||||
public void SetData<TData>(TData data){ |
|
||||||
//SaveObject = JObject.FromObject(data); |
|
||||||
if(SaveObject.ContainsKey(typeof(TData).ToString())){ |
|
||||||
SaveObject[typeof(TData).ToString()] = JToken.FromObject(data); |
|
||||||
}else{ |
|
||||||
SaveObject.Add(typeof(TData).ToString(),JToken.FromObject(data)); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary> |
#region Default Json Operation |
||||||
/// Get data from file, if your data is not in file, then get null. |
public void SetObjectToJson<TObject>(TObject obj){ |
||||||
/// </summary> |
ObjectFiles["Default/SaveData.json"].SetObject(typeof(TObject).ToString(),obj); |
||||||
/// <typeparam name="TData"></typeparam> |
|
||||||
public TData GetDataByFile<TData>() where TData : class,new(){ |
|
||||||
if(!SaveObject.ContainsKey(typeof(TData).ToString())){ |
|
||||||
return null; |
|
||||||
} |
|
||||||
TData data = SaveObject[typeof(TData).ToString()].ToObject<TData>(); |
|
||||||
return data; |
|
||||||
} |
} |
||||||
|
public TObject GetObjectFromJson<TObject>() where TObject : new(){ |
||||||
/// <summary> |
return ObjectFiles["Default/SaveData.json"].GetObject<TObject>(typeof(TObject).ToString()); |
||||||
/// Save data to file |
|
||||||
/// </summary> |
|
||||||
/// <param name="data">your any type of data</param> |
|
||||||
/// <typeparam name="TData"></typeparam> |
|
||||||
public void SetDataToFile<TData>(TData data) |
|
||||||
{ |
|
||||||
SetData(data); |
|
||||||
SaveToFile(); |
|
||||||
} |
} |
||||||
|
|
||||||
#region About Godot File's PATH |
|
||||||
// Godot's Path has res:// and user:// |
|
||||||
// UserPath is used for every platform such as android. |
|
||||||
// You can use ProjectSettings.GlobalizePath("") to convert a "local" path like res://path/to/file.txt to an absolute OS path. |
|
||||||
#endregion |
#endregion |
||||||
|
|
||||||
/// <summary> |
//------------------------------------------------------------------------------// |
||||||
/// Init a new save data file or load an other file with json suffix, if you want to load other save data, please use this function to reload; |
|
||||||
/// </summary> |
|
||||||
/// <param name="fileName"></param> |
public void OpenResPath(){ |
||||||
public void InitSaveData(string path) |
OS.ShellOpen("".GetGodotResPath()); |
||||||
{ |
|
||||||
DefaultPath = path; |
|
||||||
if(!File.Exists(path)){ |
|
||||||
File.WriteAllText(path,"{}"); |
|
||||||
} |
|
||||||
InitSaveObject(); |
|
||||||
} |
} |
||||||
} |
|
||||||
|
|
||||||
|
public void OpenUserPath(){ |
||||||
|
OS.ShellOpen("".GetGodotUserPath()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
public static class CanGetEGSaveExtension{ |
public static class CanGetEGSaveExtension{ |
||||||
public static EGSave EGSave(this IEGFramework self){ |
public static EGSave EGSave(this IEGFramework self){ |
||||||
return self.GetModule<EGSave>(); |
return self.GetModule<EGSave>(); |
||||||
} |
} |
||||||
public static EGSave EGSave(this IEGFramework self,string path){ |
|
||||||
self.GetModule<EGSave>().InitSaveData(path); |
public static string GetGodotResPath(this string absPath){ |
||||||
return self.GetModule<EGSave>(); |
return ProjectSettings.GlobalizePath("res://"+absPath); |
||||||
|
} |
||||||
|
|
||||||
|
public static string GetGodotUserPath(this string absPath){ |
||||||
|
return ProjectSettings.GlobalizePath("user://"+absPath); |
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
} |
} |
@ -0,0 +1,23 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace EGFramework |
||||||
|
{ |
||||||
|
public interface IEGSave{ |
||||||
|
void InitSaveFile(string path); |
||||||
|
} |
||||||
|
|
||||||
|
public interface IEGSaveObject{ |
||||||
|
void SetObject<TObject>(string objectKey,TObject obj); |
||||||
|
TObject GetObject<TObject>(string objectKey) where TObject : new(); |
||||||
|
} |
||||||
|
|
||||||
|
// |
||||||
|
public interface IEGSaveData{ |
||||||
|
void SetData<TData>(TData data,string dataKey,int id); |
||||||
|
TData GetData<TData>(string dataKey,int id) where TData : new(); |
||||||
|
IList<TData> QueryData<TData>(string dataKey,string sql) where TData : new(); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue