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{ @@ -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<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(){

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

@ -44,10 +44,10 @@ namespace EGFramework @@ -44,10 +44,10 @@ namespace EGFramework
public void SetObject<TObject>(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 @@ -57,10 +57,10 @@ namespace EGFramework
/// </summary>
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!");
}
TObject data = SaveObject[typeof(TObject).ToString()].ToObject<TObject>();
TObject data = SaveObject[objectKey].ToObject<TObject>();
return data;
}
}

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

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

Loading…
Cancel
Save