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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using Godot;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using System.Text.Json;
|
||||
|
||||
/// <summary>Basis JSON converter.</summary>
|
||||
public class Texture2DConverter : JsonConverter<Texture2D>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert) =>
|
||||
typeToConvert == typeof(Texture2D);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Texture2D Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options)
|
||||
{
|
||||
var texture2D = new Texture2D();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.EndObject)
|
||||
{
|
||||
return texture2D;
|
||||
}
|
||||
|
||||
if (reader.TokenType != JsonTokenType.PropertyName)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var propertyName = reader.GetString();
|
||||
reader.Read();
|
||||
|
||||
switch (propertyName)
|
||||
{
|
||||
case "resource_path":
|
||||
var resourcePath = JsonSerializer.Deserialize<string>(ref reader, options);
|
||||
texture2D = GD.Load<Texture2D>(resourcePath);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
throw new JsonException("Unexpected end when reading Texture2D.");
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(
|
||||
Utf8JsonWriter writer,
|
||||
Texture2D value,
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
var resolver = options.TypeInfoResolver;
|
||||
var resourcePathType = resolver!.GetTypeInfo(typeof(string), options)!;
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("resource_path");
|
||||
JsonSerializer.Serialize(writer, value.ResourcePath, resourcePathType);
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user