Move save logic out of Game
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
namespace Zennysoft.Game.Ma.Implementation;
|
||||
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
88
Zennysoft.Game.Ma.Implementation/EnumTypes.cs
Normal file
88
Zennysoft.Game.Ma.Implementation/EnumTypes.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Zennysoft.Game.Ma.Implementation;
|
||||
|
||||
public enum WeaponTag
|
||||
{
|
||||
None,
|
||||
SelfDamage,
|
||||
IgnoreAffinity,
|
||||
Knockback,
|
||||
}
|
||||
|
||||
[JsonSerializable(typeof(WeaponTag))]
|
||||
public partial class WeaponTagEnumContext : JsonSerializerContext;
|
||||
|
||||
public enum ItemTag
|
||||
{
|
||||
None,
|
||||
BreaksOnChange
|
||||
}
|
||||
|
||||
[JsonSerializable(typeof(ItemTag))]
|
||||
public partial class ItemTagEnumContext : JsonSerializerContext;
|
||||
|
||||
public enum AccessoryTag
|
||||
{
|
||||
None,
|
||||
HalfVTConsumption,
|
||||
StatusEffectImmunity
|
||||
}
|
||||
|
||||
[JsonSerializable(typeof(AccessoryTag))]
|
||||
public partial class AccessoryTagEnumContext : JsonSerializerContext;
|
||||
|
||||
public enum ThrowableItemTag
|
||||
{
|
||||
None,
|
||||
LowerTargetTo1HP,
|
||||
CanChangeAffinity,
|
||||
TeleportToRandomLocation,
|
||||
WarpToExitIfFound
|
||||
}
|
||||
|
||||
[JsonSerializable(typeof(ThrowableItemTag))]
|
||||
public partial class ThrowableItemTagEnumContext : JsonSerializerContext;
|
||||
|
||||
public enum UsableItemTag
|
||||
{
|
||||
None,
|
||||
DoubleEXP,
|
||||
IdentifyAllItemsCostHP,
|
||||
BriefImmunity,
|
||||
SwapHPAndVT,
|
||||
TeleportAllEnemiesToRoom,
|
||||
TurnAllEnemiesIntoHealingItem,
|
||||
KillHalfEnemiesInRoom,
|
||||
AbsorbHPFromAllEnemiesInRoom,
|
||||
HealsAllInRoomToMaxHP,
|
||||
DealElementalDamageToAllEnemiesInRoom,
|
||||
RaiseCurrentWeaponAttack,
|
||||
RaiseCurrentDefenseArmor,
|
||||
RaiseLevel,
|
||||
RandomEffect,
|
||||
}
|
||||
|
||||
[JsonSerializable(typeof(UsableItemTag))]
|
||||
public partial class UsableItemTagEnumContext : JsonSerializerContext;
|
||||
|
||||
public enum BoxItemTag
|
||||
{
|
||||
RandomNewItem,
|
||||
}
|
||||
|
||||
[JsonSerializable(typeof(BoxItemTag))]
|
||||
public partial class BoxItemTagEnumContext : JsonSerializerContext;
|
||||
|
||||
public enum ElementType
|
||||
{
|
||||
None,
|
||||
Aeolic,
|
||||
Telluric,
|
||||
Hydric,
|
||||
Igneous,
|
||||
Ferrum
|
||||
}
|
||||
|
||||
[JsonSerializable(typeof(ElementType))]
|
||||
public partial class ElementTypeEnumContext : JsonSerializerContext;
|
||||
56
Zennysoft.Game.Ma.Implementation/Save/SaveFileManager.cs
Normal file
56
Zennysoft.Game.Ma.Implementation/Save/SaveFileManager.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
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.Ma.Implementation;
|
||||
|
||||
public class SaveFileManager<T> : ISaveFileManager<T>
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
public const string SAVE_FILE_NAME = "game.json";
|
||||
public JsonSerializerOptions JsonOptions { get; set; } = default!;
|
||||
|
||||
public string SaveFilePath { get; set; } = default!;
|
||||
|
||||
public SaveFileManager(IFileSystem fileSystem)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
SaveFilePath = _fileSystem.Path.Join(OS.GetUserDataDir(), SAVE_FILE_NAME);
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public async Task<T> ReadFromFile()
|
||||
{
|
||||
if (!_fileSystem.File.Exists(SaveFilePath))
|
||||
throw new FileNotFoundException();
|
||||
|
||||
var json = await _fileSystem.File.ReadAllTextAsync(SaveFilePath);
|
||||
return JsonSerializer.Deserialize<T>(json, JsonOptions);
|
||||
}
|
||||
|
||||
public async Task WriteToFile(T gameData)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(gameData, JsonOptions);
|
||||
await _fileSystem.File.WriteAllTextAsync(SaveFilePath, json);
|
||||
}
|
||||
}
|
||||
66
Zennysoft.Game.Ma.Implementation/Texture2DConverter.cs
Normal file
66
Zennysoft.Game.Ma.Implementation/Texture2DConverter.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Chickensoft.GodotNodeInterfaces" Version="2.4.0" />
|
||||
<PackageReference Include="Chickensoft.Introspection" Version="2.2.0" />
|
||||
<PackageReference Include="Chickensoft.Introspection.Generator" Version="2.2.0" />
|
||||
<PackageReference Include="Chickensoft.Serialization.Godot" Version="0.7.6" />
|
||||
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.5.0" />
|
||||
<PackageReference Include="System.IO.Abstractions" Version="21.2.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Zennysoft.Game.Abstractions\Zennysoft.Game.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Enums\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user