|
|
|
|
using System;
|
|
|
|
|
using Godot;
|
|
|
|
|
using Godot.Collections;
|
|
|
|
|
using FileAccess = Godot.FileAccess;
|
|
|
|
|
|
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Savegame;
|
|
|
|
|
|
|
|
|
|
public static class SavegameService
|
|
|
|
|
{
|
|
|
|
|
public static readonly string SavePath = "res://savegame/savegame.json";
|
|
|
|
|
|
|
|
|
|
public static Dictionary<string, string> SaveDatas = new ();
|
|
|
|
|
|
|
|
|
|
public static bool _loaded = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void AppendSave(SaveData saveData)
|
|
|
|
|
{
|
|
|
|
|
string key = string.Concat(saveData.SceneName, "_", saveData.Id);
|
|
|
|
|
|
|
|
|
|
if (SaveDatas.TryGetValue(key, out var value))
|
|
|
|
|
{
|
|
|
|
|
SaveDatas[key] = saveData.JsonPayload;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SaveDatas.Add(key, saveData.JsonPayload);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetSaveData(string sceneName, string id)
|
|
|
|
|
{
|
|
|
|
|
string saveData = "";
|
|
|
|
|
|
|
|
|
|
string key = string.Concat(sceneName, "_", id);
|
|
|
|
|
|
|
|
|
|
if (!_loaded)
|
|
|
|
|
{
|
|
|
|
|
GD.Print("SavegameService: SaveFile not loaded.");
|
|
|
|
|
return saveData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SaveDatas.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
saveData = SaveDatas[key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return saveData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void Save()
|
|
|
|
|
{
|
|
|
|
|
string json = Json.Stringify(SaveDatas, indent: "\t");
|
|
|
|
|
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
|
|
|
|
file.StoreString(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Load()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string saveDataJson = FileAccess.GetFileAsString(SavePath);
|
|
|
|
|
SaveDatas = Json.ParseString(saveDataJson).AsGodotDictionary<string, string>();
|
|
|
|
|
}
|
|
|
|
|
catch(Exception e)
|
|
|
|
|
{
|
|
|
|
|
GD.PrintRich(e.Message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_loaded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|