♻️ reworked and debugged the SaveSystem. Removed unnecessary scene reference.
This commit is contained in:
@@ -20,11 +20,9 @@ public static class SavegameService
|
||||
public static bool _loaded = false;
|
||||
|
||||
|
||||
public static void AppendDataToSave(string scenename, string id, Dictionary<string, Variant> payload)
|
||||
public static void AppendDataToSave( string id, Dictionary<string, Variant> payload)
|
||||
{
|
||||
var saveData = new SaveData();
|
||||
|
||||
saveData.SceneName = scenename;
|
||||
saveData.Id = id;
|
||||
saveData.JsonPayload = Json.Stringify(payload, indent: "\t");
|
||||
AppendSave(saveData);
|
||||
@@ -36,15 +34,13 @@ public static class SavegameService
|
||||
/// <param name="saveData"></param>
|
||||
private static void AppendSave(SaveData saveData)
|
||||
{
|
||||
string key = string.Concat(saveData.SceneName, "_", saveData.Id);
|
||||
|
||||
if (SaveDatas.TryGetValue(key, out var value))
|
||||
if (SaveDatas.TryGetValue(saveData.Id, out var value))
|
||||
{
|
||||
SaveDatas[key] = saveData.JsonPayload;
|
||||
SaveDatas[saveData.Id] = saveData.JsonPayload;
|
||||
}
|
||||
else
|
||||
{
|
||||
SaveDatas.Add(key, saveData.JsonPayload);
|
||||
SaveDatas.Add(saveData.Id, saveData.JsonPayload);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,11 +52,10 @@ public static class SavegameService
|
||||
/// <param name="sceneName"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<string, Variant> GetSaveData(string sceneName, string id)
|
||||
public static Dictionary<string, Variant> GetSaveData(string id)
|
||||
{
|
||||
Dictionary<string, Variant> saveData = new();
|
||||
|
||||
string key = string.Concat(sceneName, "_", id);
|
||||
string saveDataString = "";
|
||||
|
||||
if (!_loaded)
|
||||
@@ -69,9 +64,9 @@ public static class SavegameService
|
||||
return saveData;
|
||||
}
|
||||
|
||||
if (SaveDatas.ContainsKey(key))
|
||||
if (SaveDatas.ContainsKey(id))
|
||||
{
|
||||
saveDataString = SaveDatas[key];
|
||||
saveDataString = SaveDatas[id];
|
||||
saveData = Json.ParseString(saveDataString).AsGodotDictionary<string, Variant>();
|
||||
}
|
||||
|
||||
@@ -88,8 +83,10 @@ public static class SavegameService
|
||||
|
||||
GD.Print(SavePath);
|
||||
// create cloud directory
|
||||
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(SavePath) ?? "");
|
||||
|
||||
CreateSaveDirectory();
|
||||
GD.Print("Save.");
|
||||
|
||||
try
|
||||
{
|
||||
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
||||
@@ -101,21 +98,28 @@ public static class SavegameService
|
||||
}
|
||||
}
|
||||
|
||||
private static void CreateSaveDirectory()
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(SavePath) ?? "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the current savegame file from disk and parses it into the SaveData dictionary.
|
||||
/// </summary>
|
||||
public static void Load()
|
||||
{
|
||||
GD.Print("Load.");
|
||||
try
|
||||
{
|
||||
if (!System.IO.File.Exists(SavePath))
|
||||
{
|
||||
SaveDatas = new();
|
||||
return;
|
||||
CreateSaveDirectory();
|
||||
}
|
||||
|
||||
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
||||
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
||||
if(!string.IsNullOrEmpty(saveDataJson))
|
||||
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
||||
|
||||
}
|
||||
catch(Exception e)
|
||||
|
||||
Reference in New Issue
Block a user