Move save logic out of Game
This commit is contained in:
@@ -14,6 +14,10 @@ using System.IO.Abstractions;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using static Zennysoft.Game.Ma.GameLogic.State;
|
||||
using Zennysoft.Game.Abstractions;
|
||||
using Zennysoft.Game.Ma.Implementation;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
|
||||
[Meta(typeof(IAutoNode))]
|
||||
public partial class Game : Node3D, IGame
|
||||
@@ -28,6 +32,8 @@ public partial class Game : Node3D, IGame
|
||||
|
||||
IGameEventDepot IProvide<IGameEventDepot>.Value() => GameEventDepot;
|
||||
|
||||
private static SimpleInjector.Container _container;
|
||||
|
||||
public IInstantiator Instantiator { get; set; } = default!;
|
||||
|
||||
public IGameLogic GameLogic { get; set; } = default!;
|
||||
@@ -64,12 +70,6 @@ public partial class Game : Node3D, IGame
|
||||
#region Save
|
||||
public JsonSerializerOptions JsonOptions { get; set; } = default!;
|
||||
|
||||
public const string SAVE_FILE_NAME = "game.json";
|
||||
|
||||
public IFileSystem FileSystem { get; set; } = default!;
|
||||
|
||||
public string SaveFilePath { get; set; } = default!;
|
||||
|
||||
public ISaveFile<GameData> SaveFile { get; set; } = default!;
|
||||
|
||||
public ISaveChunk<GameData> GameChunk { get; set; } = default!;
|
||||
@@ -86,9 +86,10 @@ public partial class Game : Node3D, IGame
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
FileSystem = new FileSystem();
|
||||
|
||||
SaveFilePath = FileSystem.Path.Join(OS.GetUserDataDir(), SAVE_FILE_NAME);
|
||||
_container = new SimpleInjector.Container();
|
||||
_container.Register<IFileSystem, FileSystem>();
|
||||
_container.Register<ISaveFileManager<GameData>, SaveFileManager<GameData>>();
|
||||
_container.Verify();
|
||||
|
||||
GameRepo = new GameRepo();
|
||||
GameLogic = new GameLogic();
|
||||
@@ -101,21 +102,6 @@ public partial class Game : Node3D, IGame
|
||||
Instantiator = new Instantiator(GetTree());
|
||||
RescuedItems = new RescuedItemDatabase();
|
||||
|
||||
var resolver = new SerializableTypeResolver();
|
||||
GodotSerialization.Setup();
|
||||
Serializer.AddConverter(new Texture2DConverter());
|
||||
|
||||
var upgradeDependencies = new Blackboard();
|
||||
|
||||
JsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
Converters = {
|
||||
new SerializableTypeConverter(upgradeDependencies)
|
||||
},
|
||||
TypeInfoResolver = JsonTypeInfoResolver.Combine(resolver, WeaponTagEnumContext.Default, ItemTagEnumContext.Default, ElementTypeEnumContext.Default, AccessoryTagEnumContext.Default, ThrowableItemTagEnumContext.Default, UsableItemTagEnumContext.Default, BoxItemTagEnumContext.Default),
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
GameChunk = new SaveChunk<GameData>(
|
||||
(chunk) =>
|
||||
{
|
||||
@@ -162,25 +148,23 @@ public partial class Game : Node3D, IGame
|
||||
|
||||
public void OnResolved()
|
||||
{
|
||||
var saveFileManager = _container.GetInstance<ISaveFileManager<GameData>>();
|
||||
SaveFile = new SaveFile<GameData>(
|
||||
root: GameChunk,
|
||||
onSave: async (GameData data) =>
|
||||
{
|
||||
// Save the game data to disk.
|
||||
var json = JsonSerializer.Serialize(data, JsonOptions);
|
||||
await FileSystem.File.WriteAllTextAsync(SaveFilePath, json);
|
||||
},
|
||||
onSave: saveFileManager.WriteToFile,
|
||||
onLoad: async () =>
|
||||
{
|
||||
// Load the game data from disk.
|
||||
if (!FileSystem.File.Exists(SaveFilePath))
|
||||
try
|
||||
{
|
||||
GD.Print("No save file to load");
|
||||
return null;
|
||||
var gameData = await saveFileManager.ReadFromFile();
|
||||
return gameData;
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
GD.Print("No save file found.");
|
||||
}
|
||||
|
||||
var json = await FileSystem.File.ReadAllTextAsync(SaveFilePath);
|
||||
return JsonSerializer.Deserialize<GameData>(json, JsonOptions);
|
||||
return null;
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user