Browse Source

fixed jsonsave to key value pair

master
jkpete 2 months ago
parent
commit
35c7a5fb8c
  1. 8
      Example/UsingTest/Script/EGSaveTest.cs
  2. 10
      addons/EGFramework/Module/SaveTools/EGJsonSave.cs
  3. 27
      addons/EGFramework/Module/SaveTools/EGSave.cs

8
Example/UsingTest/Script/EGSaveTest.cs

@ -9,11 +9,17 @@ namespace EGFramework.Examples.Test{
public override void _Ready() public override void _Ready()
{ {
base._Ready(); base._Ready();
this.EGSave().OpenUserPath(); // this.EGSave().OpenUserPath();
// GD.Print(ProjectSettings.GlobalizePath("res://SaveData/Default.json")); // GD.Print(ProjectSettings.GlobalizePath("res://SaveData/Default.json"));
// GD.Print(ProjectSettings.GlobalizePath("user://SaveData/Default.json")); // GD.Print(ProjectSettings.GlobalizePath("user://SaveData/Default.json"));
// GD.Print(Path.GetDirectoryName(ProjectSettings.GlobalizePath("res://SaveData/Default.json"))); // GD.Print(Path.GetDirectoryName(ProjectSettings.GlobalizePath("res://SaveData/Default.json")));
// TestLiteDB(); // TestLiteDB();
string CardPath1 = "SaveData/CardData1.json".GetGodotResPath();
this.EGSave().LoadObjectFile<EGJsonSave>(CardPath1);
// this.EGSave().SetObject(CardPath1,"Customer1",new Customer() { Name = "Andy" });
// this.EGSave().SetObject(CardPath1,"Customer3",new Customer() { Name = "Terry" });
Customer customer = this.EGSave().GetObject<Customer>(CardPath1,"Customer3");
GD.Print("ReadName is "+customer.Name);
} }
public void TestSqlite(){ public void TestSqlite(){

10
addons/EGFramework/Module/SaveTools/EGJsonSave.cs

@ -44,10 +44,10 @@ namespace EGFramework
public void SetObject<TObject>(string objectKey,TObject obj) public void SetObject<TObject>(string objectKey,TObject obj)
{ {
if(SaveObject.ContainsKey(typeof(TObject).ToString())){ if(SaveObject.ContainsKey(objectKey)){
SaveObject[typeof(TObject).ToString()] = JToken.FromObject(obj); SaveObject[objectKey] = JToken.FromObject(obj);
}else{ }else{
SaveObject.Add(typeof(TObject).ToString(),JToken.FromObject(obj)); SaveObject.Add(objectKey,JToken.FromObject(obj));
} }
File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented)); File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented));
} }
@ -57,10 +57,10 @@ namespace EGFramework
/// </summary> /// </summary>
public TObject GetObject<TObject>(string objectKey) where TObject : new() public TObject GetObject<TObject>(string objectKey) where TObject : new()
{ {
if(!SaveObject.ContainsKey(typeof(TObject).ToString())){ if(!SaveObject.ContainsKey(objectKey)){
throw new Exception("Key not found!"); throw new Exception("Key not found!");
} }
TObject data = SaveObject[typeof(TObject).ToString()].ToObject<TObject>(); TObject data = SaveObject[objectKey].ToObject<TObject>();
return data; return data;
} }
} }

27
addons/EGFramework/Module/SaveTools/EGSave.cs

@ -30,28 +30,19 @@ namespace EGFramework
public EGSave() {} public EGSave() {}
public override void Init() public override void Init()
{ {
LoadObjectFile("SaveData/DefaultJsonSave.json".GetGodotResPath(),TypeEGSave.Json); LoadObjectFile<EGJsonSave>("SaveData/DefaultJsonSave.json".GetGodotResPath());
} }
public void LoadDataFile(string path,TypeDBSave type){ public void LoadDataFile<TSaveData>(string path) where TSaveData:IEGSaveData,IEGSave,new(){
switch(type){ TSaveData saveData = new TSaveData();
case TypeDBSave.Csv: saveData.InitSaveFile(path);
break; DataBaseFiles.Add(path,saveData);
default:
break;
}
} }
public void LoadObjectFile(string path,TypeEGSave type){ public void LoadObjectFile<TSaveObject>(string path) where TSaveObject:IEGSaveObject,IEGSave,new(){
switch(type){ TSaveObject saveObject = new TSaveObject();
case TypeEGSave.Json: saveObject.InitSaveFile(path);
EGJsonSave newJsonFile = new EGJsonSave(); ObjectFiles.Add(path, saveObject);
newJsonFile.InitSaveFile(path);
ObjectFiles.Add(path, newJsonFile);
break;
default:
break;
}
} }
public void SetObject<TObject>(string path,string objectKey,TObject obj){ public void SetObject<TObject>(string path,string objectKey,TObject obj){

Loading…
Cancel
Save