From b71b12185235040bd83520aef33b8a8d3fd585ec Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Wed, 2 Oct 2024 11:20:00 +0800 Subject: [PATCH] add eglitedbSave --- .../Module/SaveTools/EGByteSave.cs | 6 +- addons/EGFramework/Module/SaveTools/EGCsv.cs | 15 ---- .../EGFramework/Module/SaveTools/EGCsvSave.cs | 39 ++++++++++ .../Module/SaveTools/EGJsonSave.cs | 2 +- .../EGFramework/Module/SaveTools/EGLiteDB.cs | 16 ----- .../Module/SaveTools/EGLiteDBSave.cs | 71 +++++++++++++++++++ addons/EGFramework/Module/SaveTools/EGSave.cs | 13 +++- .../Module/SaveTools/SaveToolsInterface.cs | 9 ++- 8 files changed, 128 insertions(+), 43 deletions(-) delete mode 100644 addons/EGFramework/Module/SaveTools/EGCsv.cs create mode 100644 addons/EGFramework/Module/SaveTools/EGCsvSave.cs delete mode 100644 addons/EGFramework/Module/SaveTools/EGLiteDB.cs create mode 100644 addons/EGFramework/Module/SaveTools/EGLiteDBSave.cs diff --git a/addons/EGFramework/Module/SaveTools/EGByteSave.cs b/addons/EGFramework/Module/SaveTools/EGByteSave.cs index 78f2f19..86ed428 100644 --- a/addons/EGFramework/Module/SaveTools/EGByteSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGByteSave.cs @@ -7,7 +7,7 @@ using Godot; namespace EGFramework { - public class EGByteSave : EGModule, IEGSave,IEGSaveObject + public class EGByteSave : IEGSave,IEGSaveObject { public void SaveToFile(string content) @@ -26,10 +26,6 @@ namespace EGFramework Variant pos = file.GetVar(); return pos; } - public override void Init() - { - - } public void InitSaveFile(string path) { diff --git a/addons/EGFramework/Module/SaveTools/EGCsv.cs b/addons/EGFramework/Module/SaveTools/EGCsv.cs deleted file mode 100644 index 85c8160..0000000 --- a/addons/EGFramework/Module/SaveTools/EGCsv.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace EGFramework -{ - public class EGCsv : EGModule - { - public override void Init() - { - - } - } -} \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGCsvSave.cs b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs new file mode 100644 index 0000000..31506df --- /dev/null +++ b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; + +namespace EGFramework +{ + public class EGCsvSave : IEGSaveData, IEGSave + { + public void InitSaveFile(string path) + { + throw new NotImplementedException(); + } + + public void SetData(string dataKey, TData data, object id) + { + throw new NotImplementedException(); + } + public TData GetData(string dataKey, object id) where TData : new() + { + throw new NotImplementedException(); + } + public IEnumerable QueryData(string dataKey, string sql) where TData : new() + { + throw new NotImplementedException(); + } + + public IEnumerable GetAll(string dataKey) where TData : new() + { + throw new NotImplementedException(); + } + + public IEnumerable FindData(string dataKey, Expression> expression) where TData : new() + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGJsonSave.cs b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs index c9dd31a..2c174e7 100644 --- a/addons/EGFramework/Module/SaveTools/EGJsonSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs @@ -28,7 +28,7 @@ namespace EGFramework { DefaultPath = path; if(!File.Exists(path)){ - if (!Directory.Exists(DefaultPath)) + if (!Directory.Exists(Path.GetDirectoryName(DefaultPath))) { Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath)); File.WriteAllText(DefaultPath,"{}"); diff --git a/addons/EGFramework/Module/SaveTools/EGLiteDB.cs b/addons/EGFramework/Module/SaveTools/EGLiteDB.cs deleted file mode 100644 index 999416c..0000000 --- a/addons/EGFramework/Module/SaveTools/EGLiteDB.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using LiteDB; - -namespace EGFramework -{ - public class EGLiteDB : EGModule - { - public override void Init() - { - // throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGLiteDBSave.cs b/addons/EGFramework/Module/SaveTools/EGLiteDBSave.cs new file mode 100644 index 0000000..ef949d6 --- /dev/null +++ b/addons/EGFramework/Module/SaveTools/EGLiteDBSave.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using LiteDB; + +namespace EGFramework +{ + public class EGLiteDBSave : IEGSave,IEGSaveData + { + private string DefaultPath { set; get; } + private LiteDatabase _Database { set; get; } + private LiteDatabase Database{ + get { + if(_Database == null){ + InitSaveFile(DefaultPath); + } + return _Database; + } + } + + public void InitSaveFile(string path) + { + DefaultPath = path; + if (!Directory.Exists(Path.GetDirectoryName(DefaultPath))) + { + Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath)); + } + // Default is "SaveData/DefaultLiteDBData.db" + _Database = new LiteDatabase(path); + } + + public TData GetData(string dataKey, object id) where TData : new() + { + LiteCollection collection = (LiteCollection)Database.GetCollection(dataKey); + TData result = collection.FindById((BsonValue)id); + return result; + } + + public void SetData(string dataKey, TData data, object id) + { + LiteCollection collection = (LiteCollection)Database.GetCollection(dataKey); + if(collection.FindById((BsonValue)id)==null){ + collection.Insert((BsonValue)id, data); + } + collection.Update(data); + } + + public IEnumerable GetAll(string dataKey) where TData : new() + { + LiteCollection collection = (LiteCollection)Database.GetCollection(dataKey); + return collection.FindAll(); + } + + public IEnumerable FindData(string dataKey, Expression> expression) where TData : new() + { + LiteCollection collection = (LiteCollection)Database.GetCollection(dataKey); + return collection.Find(expression); + } + + public IEnumerable QueryData(string dataKey, string sql="") where TData : new() + { + LiteCollection collection = (LiteCollection)Database.GetCollection(dataKey); + return collection.FindAll(); + } + + + } +} \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGSave.cs b/addons/EGFramework/Module/SaveTools/EGSave.cs index 798b0a8..7d67248 100644 --- a/addons/EGFramework/Module/SaveTools/EGSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGSave.cs @@ -30,7 +30,7 @@ namespace EGFramework public EGSave() {} public override void Init() { - LoadObjectFile("Default/SaveData.json".GetGodotUserPath(),TypeEGSave.Json); + LoadObjectFile("SaveData/DefaultJsonSave.json".GetGodotResPath(),TypeEGSave.Json); } public void LoadDataFile(string path,TypeDBSave type){ @@ -61,14 +61,21 @@ namespace EGFramework return ObjectFiles[path].GetObject(key); } + public void SetData(string path,string dataKey,TData data,int id){ + DataBaseFiles[path].SetData(dataKey,data,id); + } + public void GetData(string path,string key,int id) where TData : new(){ + DataBaseFiles[path].GetData(key,id); + } + //------------------------------------------------------------------------------// #region Default Json Operation public void SetObjectToJson(TObject obj){ - ObjectFiles["Default/SaveData.json"].SetObject(typeof(TObject).ToString(),obj); + ObjectFiles["SaveData/DefaultJsonSave.json"].SetObject(typeof(TObject).ToString(),obj); } public TObject GetObjectFromJson() where TObject : new(){ - return ObjectFiles["Default/SaveData.json"].GetObject(typeof(TObject).ToString()); + return ObjectFiles["SaveData/DefaultJsonSave.json"].GetObject(typeof(TObject).ToString()); } #endregion diff --git a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs index bb538a1..75c54a8 100644 --- a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs +++ b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; namespace EGFramework @@ -16,8 +17,10 @@ namespace EGFramework // public interface IEGSaveData{ - void SetData(TData data,string dataKey,int id); - TData GetData(string dataKey,int id) where TData : new(); - IList QueryData(string dataKey,string sql) where TData : new(); + void SetData(string dataKey,TData data,object id); + TData GetData(string dataKey,object id) where TData : new(); + IEnumerable GetAll(string dataKey) where TData : new(); + IEnumerable FindData(string dataKey,Expression> expression) where TData : new(); + IEnumerable QueryData(string dataKey,string sql) where TData : new(); } } \ No newline at end of file