From 35c7a5fb8cb38f2273cf915f76811aa95de15767 Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Thu, 3 Oct 2024 00:04:44 +0800 Subject: [PATCH] fixed jsonsave to key value pair --- Example/UsingTest/Script/EGSaveTest.cs | 8 +++++- .../Module/SaveTools/EGJsonSave.cs | 10 +++---- addons/EGFramework/Module/SaveTools/EGSave.cs | 27 +++++++------------ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs index 9c1666d..f1bc5b9 100644 --- a/Example/UsingTest/Script/EGSaveTest.cs +++ b/Example/UsingTest/Script/EGSaveTest.cs @@ -9,11 +9,17 @@ namespace EGFramework.Examples.Test{ public override void _Ready() { base._Ready(); - this.EGSave().OpenUserPath(); + // this.EGSave().OpenUserPath(); // GD.Print(ProjectSettings.GlobalizePath("res://SaveData/Default.json")); // GD.Print(ProjectSettings.GlobalizePath("user://SaveData/Default.json")); // GD.Print(Path.GetDirectoryName(ProjectSettings.GlobalizePath("res://SaveData/Default.json"))); // TestLiteDB(); + string CardPath1 = "SaveData/CardData1.json".GetGodotResPath(); + this.EGSave().LoadObjectFile(CardPath1); + // this.EGSave().SetObject(CardPath1,"Customer1",new Customer() { Name = "Andy" }); + // this.EGSave().SetObject(CardPath1,"Customer3",new Customer() { Name = "Terry" }); + Customer customer = this.EGSave().GetObject(CardPath1,"Customer3"); + GD.Print("ReadName is "+customer.Name); } public void TestSqlite(){ diff --git a/addons/EGFramework/Module/SaveTools/EGJsonSave.cs b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs index 2c174e7..eb172ad 100644 --- a/addons/EGFramework/Module/SaveTools/EGJsonSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs @@ -44,10 +44,10 @@ namespace EGFramework public void SetObject(string objectKey,TObject obj) { - if(SaveObject.ContainsKey(typeof(TObject).ToString())){ - SaveObject[typeof(TObject).ToString()] = JToken.FromObject(obj); + if(SaveObject.ContainsKey(objectKey)){ + SaveObject[objectKey] = JToken.FromObject(obj); }else{ - SaveObject.Add(typeof(TObject).ToString(),JToken.FromObject(obj)); + SaveObject.Add(objectKey,JToken.FromObject(obj)); } File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented)); } @@ -57,10 +57,10 @@ namespace EGFramework /// public TObject GetObject(string objectKey) where TObject : new() { - if(!SaveObject.ContainsKey(typeof(TObject).ToString())){ + if(!SaveObject.ContainsKey(objectKey)){ throw new Exception("Key not found!"); } - TObject data = SaveObject[typeof(TObject).ToString()].ToObject(); + TObject data = SaveObject[objectKey].ToObject(); return data; } } diff --git a/addons/EGFramework/Module/SaveTools/EGSave.cs b/addons/EGFramework/Module/SaveTools/EGSave.cs index 7d67248..28f8d04 100644 --- a/addons/EGFramework/Module/SaveTools/EGSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGSave.cs @@ -30,28 +30,19 @@ namespace EGFramework public EGSave() {} public override void Init() { - LoadObjectFile("SaveData/DefaultJsonSave.json".GetGodotResPath(),TypeEGSave.Json); + LoadObjectFile("SaveData/DefaultJsonSave.json".GetGodotResPath()); } - public void LoadDataFile(string path,TypeDBSave type){ - switch(type){ - case TypeDBSave.Csv: - break; - default: - break; - } + public void LoadDataFile(string path) where TSaveData:IEGSaveData,IEGSave,new(){ + TSaveData saveData = new TSaveData(); + saveData.InitSaveFile(path); + DataBaseFiles.Add(path,saveData); } - 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; - } + public void LoadObjectFile(string path) where TSaveObject:IEGSaveObject,IEGSave,new(){ + TSaveObject saveObject = new TSaveObject(); + saveObject.InitSaveFile(path); + ObjectFiles.Add(path, saveObject); } public void SetObject(string path,string objectKey,TObject obj){