jkpete
2 months ago
14 changed files with 190 additions and 124 deletions
@ -0,0 +1,67 @@
@@ -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 @@
@@ -1,131 +1,103 @@
|
||||
using System; |
||||
using System.IO; |
||||
using System.Collections.Generic; |
||||
using Newtonsoft.Json; |
||||
using Newtonsoft.Json.Bson; |
||||
using Newtonsoft.Json.Linq; |
||||
using Godot; |
||||
|
||||
namespace EGFramework |
||||
{ |
||||
public interface IEGSave{ |
||||
void SetDataToFile<TData>(TData data); |
||||
TData GetDataByFile<TData>() where TData : class,new(); |
||||
void InitSaveData(string path); |
||||
} |
||||
public enum TypeEGSave{ |
||||
Json = 0, |
||||
Bson = 1, |
||||
Byte = 2, |
||||
Sqlite = 3, |
||||
LiteDB = 4, |
||||
XML = 5 |
||||
XML = 3 |
||||
} |
||||
public enum TypeDBSave{ |
||||
Csv = 0, |
||||
Sqlite = 1, |
||||
LiteDB = 2, |
||||
} |
||||
|
||||
public class EGSave : EGModule,IEGSave |
||||
public class EGSave : EGModule |
||||
{ |
||||
private string DefaultPath = "Default/SaveData.json"; |
||||
private JObject _SaveObject; |
||||
private JObject SaveObject{ |
||||
get { |
||||
if(_SaveObject == null){ |
||||
InitSaveObject(); |
||||
} |
||||
return _SaveObject; |
||||
} |
||||
} |
||||
#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 |
||||
|
||||
private Dictionary<string,IEGSaveData> DataBaseFiles = new Dictionary<string,IEGSaveData>(); |
||||
private Dictionary<string,IEGSaveObject> ObjectFiles = new Dictionary<string,IEGSaveObject>(); |
||||
public EGSave() {} |
||||
public override void Init() |
||||
{ |
||||
if (!Directory.Exists(DefaultPath)) |
||||
{ |
||||
Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath)); |
||||
File.WriteAllText(DefaultPath,"{}"); |
||||
}else if(!File.Exists(DefaultPath)){ |
||||
File.WriteAllText(DefaultPath,"{}"); |
||||
LoadObjectFile("Default/SaveData.json".GetGodotUserPath(),TypeEGSave.Json); |
||||
} |
||||
|
||||
public void LoadDataFile(string path,TypeDBSave type){ |
||||
switch(type){ |
||||
case TypeDBSave.Csv: |
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
} |
||||
|
||||
private void InitSaveObject(){ |
||||
using (StreamReader reader = File.OpenText(DefaultPath)) |
||||
{ |
||||
_SaveObject = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); |
||||
public void LoadObjectFile(string path,TypeEGSave type){ |
||||
switch(type){ |
||||
case TypeEGSave.Json: |
||||
EGJsonSave newJsonFile = new EGJsonSave(); |
||||
newJsonFile.InitSaveFile(path); |
||||
ObjectFiles.Add(path, newJsonFile); |
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Push SaveObject data set to file |
||||
/// </summary> |
||||
public void SaveToFile(){ |
||||
SaveToFile(DefaultPath); |
||||
public void SetObject<TObject>(string path,string objectKey,TObject obj){ |
||||
ObjectFiles[path].SetObject(objectKey,obj); |
||||
} |
||||
private void SaveToFile(string fileName){ |
||||
File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented)); |
||||
public TObject GetObject<TObject>(string path,string key) where TObject : new(){ |
||||
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> |
||||
/// Get data from file, if your data is not in file, then get null. |
||||
/// </summary> |
||||
/// <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; |
||||
#region Default Json Operation |
||||
public void SetObjectToJson<TObject>(TObject obj){ |
||||
ObjectFiles["Default/SaveData.json"].SetObject(typeof(TObject).ToString(),obj); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// 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(); |
||||
public TObject GetObjectFromJson<TObject>() where TObject : new(){ |
||||
return ObjectFiles["Default/SaveData.json"].GetObject<TObject>(typeof(TObject).ToString()); |
||||
} |
||||
|
||||
#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 |
||||
|
||||
/// <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 InitSaveData(string path) |
||||
{ |
||||
DefaultPath = path; |
||||
if(!File.Exists(path)){ |
||||
File.WriteAllText(path,"{}"); |
||||
} |
||||
InitSaveObject(); |
||||
//------------------------------------------------------------------------------// |
||||
|
||||
|
||||
public void OpenResPath(){ |
||||
OS.ShellOpen("".GetGodotResPath()); |
||||
} |
||||
} |
||||
|
||||
public void OpenUserPath(){ |
||||
OS.ShellOpen("".GetGodotUserPath()); |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
public static class CanGetEGSaveExtension{ |
||||
public static EGSave EGSave(this IEGFramework self){ |
||||
return self.GetModule<EGSave>(); |
||||
} |
||||
public static EGSave EGSave(this IEGFramework self,string path){ |
||||
self.GetModule<EGSave>().InitSaveData(path); |
||||
return self.GetModule<EGSave>(); |
||||
|
||||
public static string GetGodotResPath(this string absPath){ |
||||
return ProjectSettings.GlobalizePath("res://"+absPath); |
||||
} |
||||
|
||||
public static string GetGodotUserPath(this string absPath){ |
||||
return ProjectSettings.GlobalizePath("user://"+absPath); |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,23 @@
@@ -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