Fix texture stuff
This commit is contained in:
70
Zennysoft.Game.Godot.Implementation/Save/SaveFileManager.cs
Normal file
70
Zennysoft.Game.Godot.Implementation/Save/SaveFileManager.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Chickensoft.Collections;
|
||||
using Chickensoft.Serialization.Godot;
|
||||
using Chickensoft.Serialization;
|
||||
using Godot;
|
||||
using System.IO.Abstractions;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using Zennysoft.Game.Abstractions;
|
||||
|
||||
namespace Zennysoft.Game.Implementation;
|
||||
|
||||
public class SaveFileManager<T> : ISaveFileManager<T>
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private readonly JsonSerializerOptions _jsonOptions;
|
||||
private string _defaultSaveLocation;
|
||||
public const string DEFAULT_SAVE_FILE_NAME = "game.json";
|
||||
|
||||
public SaveFileManager(IFileSystem fileSystem)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
_defaultSaveLocation = _fileSystem.Path.Join(OS.GetUserDataDir(), DEFAULT_SAVE_FILE_NAME);
|
||||
|
||||
GodotSerialization.Setup();
|
||||
Serializer.AddConverter(new Texture2DConverter());
|
||||
|
||||
var upgradeDependencies = new Blackboard();
|
||||
|
||||
_jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
Converters = {
|
||||
new SerializableTypeConverter(upgradeDependencies)
|
||||
},
|
||||
WriteIndented = true
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public Task<T?> ReadFromFile(params IJsonTypeInfoResolver?[] resolvers)
|
||||
{
|
||||
if (!_fileSystem.File.Exists(_defaultSaveLocation))
|
||||
throw new FileNotFoundException();
|
||||
return ReadFromFile(_defaultSaveLocation, resolvers);
|
||||
}
|
||||
|
||||
public async Task<T?> ReadFromFile(string filePath, params IJsonTypeInfoResolver?[] resolvers)
|
||||
{
|
||||
if (!_fileSystem.File.Exists(filePath))
|
||||
throw new FileNotFoundException();
|
||||
|
||||
var json = await _fileSystem.File.ReadAllTextAsync(filePath);
|
||||
|
||||
var resolver = new SerializableTypeResolver();
|
||||
_jsonOptions.TypeInfoResolver = JsonTypeInfoResolver.Combine([resolver, .. resolvers]);
|
||||
return JsonSerializer.Deserialize<T?>(json, _jsonOptions);
|
||||
}
|
||||
|
||||
public Task WriteToFile(T gameData, params IJsonTypeInfoResolver?[] resolvers)
|
||||
{
|
||||
return WriteToFile(gameData, _defaultSaveLocation, resolvers);
|
||||
}
|
||||
|
||||
public async Task WriteToFile(T gameData, string filePath, params IJsonTypeInfoResolver?[] resolvers)
|
||||
{
|
||||
var resolver = new SerializableTypeResolver();
|
||||
_jsonOptions.TypeInfoResolver = JsonTypeInfoResolver.Combine([resolver, .. resolvers]);
|
||||
var json = JsonSerializer.Serialize(gameData, _jsonOptions);
|
||||
await _fileSystem.File.WriteAllTextAsync(filePath, json);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user