Move save logic out of Game

This commit is contained in:
2025-03-07 00:32:19 -08:00
parent a09f6ec5a5
commit 3b5ee84ce2
39 changed files with 212 additions and 110 deletions

14
Ma.sln
View File

@@ -3,15 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35222.181
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Zennysoft.Game.Ma", "Zennysoft.Game.Ma\Zennysoft.Game.Ma.csproj", "{B685AA99-B971-46A7-A708-00546BA0EF55}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Zennysoft.Game.Ma", "Zennysoft.Game.Ma\Ma.csproj", "{B685AA99-B971-46A7-A708-00546BA0EF55}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zennysoft.Game.Ma.Implementation", "Zennysoft.Game.Ma.Implementation\Zennysoft.Game.Ma.Implementation.csproj", "{3C934960-1375-4971-BF3F-C21F5241573A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Zennysoft.Game.Ma.Implementation", "Zennysoft.Game.Ma.Implementation\Zennysoft.Game.Ma.Implementation.csproj", "{3C934960-1375-4971-BF3F-C21F5241573A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E4C0167B-02AB-49E0-B36D-30D0A2479C25}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zennysoft.Game.Abstractions", "Zennysoft.Game.Abstractions\Zennysoft.Game.Abstractions.csproj", "{EB9A8B79-82E1-4254-B4FB-7F4BCB57BDBF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -36,6 +38,14 @@ Global
{3C934960-1375-4971-BF3F-C21F5241573A}.ExportRelease|Any CPU.Build.0 = Release|Any CPU
{3C934960-1375-4971-BF3F-C21F5241573A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C934960-1375-4971-BF3F-C21F5241573A}.Release|Any CPU.Build.0 = Release|Any CPU
{EB9A8B79-82E1-4254-B4FB-7F4BCB57BDBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB9A8B79-82E1-4254-B4FB-7F4BCB57BDBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB9A8B79-82E1-4254-B4FB-7F4BCB57BDBF}.ExportDebug|Any CPU.ActiveCfg = Debug|Any CPU
{EB9A8B79-82E1-4254-B4FB-7F4BCB57BDBF}.ExportDebug|Any CPU.Build.0 = Debug|Any CPU
{EB9A8B79-82E1-4254-B4FB-7F4BCB57BDBF}.ExportRelease|Any CPU.ActiveCfg = Release|Any CPU
{EB9A8B79-82E1-4254-B4FB-7F4BCB57BDBF}.ExportRelease|Any CPU.Build.0 = Release|Any CPU
{EB9A8B79-82E1-4254-B4FB-7F4BCB57BDBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB9A8B79-82E1-4254-B4FB-7F4BCB57BDBF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -0,0 +1,10 @@
using System.Text.Json;
namespace Zennysoft.Game.Abstractions;
public interface ISaveFileManager<T>
{
public Task WriteToFile(T gameData);
public Task<T> ReadFromFile();
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -1,6 +0,0 @@
namespace Zennysoft.Game.Ma.Implementation;
public class Class1
{
}

View File

@@ -1,6 +1,36 @@
using System.Text.Json.Serialization;
namespace Zennysoft.Game.Ma;
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
{
@@ -43,3 +73,16 @@ public enum BoxItemTag
[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;

View 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);
}
}

View File

@@ -1,8 +1,7 @@
using Godot;
using Godot;
using System.Text.Json.Serialization;
using System.Text.Json;
using System;
/// <summary>Basis JSON converter.</summary>
public class Texture2DConverter : JsonConverter<Texture2D>

View File

@@ -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>

View File

@@ -5,6 +5,10 @@
<!-- Use NativeAOT. -->
<PublishAot>true</PublishAot>
</PropertyGroup>
<ItemGroup>
<Compile Remove="src\items\weapons\models\**" />
<EmbeddedResource Remove="src\items\weapons\models\**" />
</ItemGroup>
<ItemGroup>
<!-- Root the assemblies to avoid trimming. -->
<TrimmerRootAssembly Include="GodotSharp" />
@@ -20,17 +24,21 @@
<PackageReference Include="Chickensoft.SaveFileBuilder" Version="1.1.0" />
<PackageReference Include="Chickensoft.Serialization.Godot" Version="0.7.6" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.5.0" />
<PackageReference Include="SimpleInjector" Version="5.5.0" />
<PackageReference Include="SSH.NET" Version="2024.2.0" />
<PackageReference Include="System.IO.Abstractions" Version="21.2.1" />
<PackageReference Include="Zeroconf" Version="3.7.16" />
</ItemGroup>
<ItemGroup>
<Folder Include="src\items\weapons\models\" />
<Folder Include="src\ui\dialogue\" />
</ItemGroup>
<ItemGroup>
<None Include=".editorconfig" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Zennysoft.Game.Abstractions\Zennysoft.Game.Abstractions.csproj" />
<ProjectReference Include="..\Zennysoft.Game.Ma.Implementation\Zennysoft.Game.Ma.Implementation.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Godot.SourceGenerators" Version="4.4.0-dev.2" />
</ItemGroup>

View File

@@ -6,14 +6,8 @@ using Godot;
using System.Reflection;
#endif
// This entry-point file is responsible for determining if we should run tests.
//
// If you want to edit your game's main entry-point, please see Game.tscn and
// Game.cs instead.
public partial class Main : Node
{
public override void _Ready()
{
// If we don't need to run tests, we can just switch to the game scene.

View File

@@ -3,6 +3,7 @@ using Chickensoft.Collections;
using Chickensoft.GodotNodeInterfaces;
using Chickensoft.Introspection;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -3,6 +3,7 @@ using Chickensoft.Collections;
using Chickensoft.Introspection;
using Godot;
using System.Linq;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,4 +1,5 @@
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,23 +0,0 @@
using System.Text.Json.Serialization;
namespace Zennysoft.Game.Ma;
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;

View File

@@ -2,6 +2,7 @@ using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Zennysoft.Game.Ma.src.enemy;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -2,6 +2,7 @@ using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Zennysoft.Game.Ma.src.enemy;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -3,6 +3,7 @@ using Chickensoft.Introspection;
using Godot;
using System;
using System.Collections.Generic;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -3,6 +3,7 @@ using Chickensoft.Introspection;
using Godot;
using System.Collections.Generic;
using System;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,6 +1,7 @@
using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -2,6 +2,7 @@ using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Zennysoft.Game.Ma.src.enemy;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -3,6 +3,7 @@ using Chickensoft.Introspection;
using Godot;
using System.Collections.Generic;
using System;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,16 +0,0 @@
using System.Text.Json.Serialization;
namespace Zennysoft.Game.Ma;
public enum ElementType
{
None,
Aeolic,
Telluric,
Hydric,
Igneous,
Ferrum
}
[JsonSerializable(typeof(ElementType))]
public partial class ElementTypeEnumContext : JsonSerializerContext;

View File

@@ -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;
}
);

View File

@@ -1,6 +1,7 @@
using Godot;
using System.Linq;
using System;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma.src.items;

View File

@@ -1,5 +1,6 @@
using Chickensoft.Introspection;
using Chickensoft.Serialization;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,6 +1,7 @@
using Chickensoft.Introspection;
using Chickensoft.Serialization;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,5 +1,6 @@
using Chickensoft.Introspection;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,7 +1,7 @@
using Chickensoft.Introspection;
using Chickensoft.Serialization;
using Godot;
using System.Text.Json.Serialization;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;
@@ -32,13 +32,4 @@ public partial class AccessoryStats : InventoryItemStats
[Export]
[Save("accessory_tag")]
public AccessoryTag AccessoryTag { get; set; } = AccessoryTag.None;
}
public enum AccessoryTag
{
None,
HalfVTConsumption,
StatusEffectImmunity
}
[JsonSerializable(typeof(AccessoryTag))]
public partial class AccessoryTagEnumContext : JsonSerializerContext;
}

View File

@@ -1,5 +1,6 @@
using Chickensoft.Introspection;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,5 +1,6 @@
using Chickensoft.Introspection;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,6 +1,7 @@
using Chickensoft.Introspection;
using Chickensoft.Serialization;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,5 +1,6 @@
using Chickensoft.Introspection;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,6 +1,7 @@
using Chickensoft.Introspection;
using Chickensoft.Serialization;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -2,6 +2,7 @@ using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Zennysoft.Game.Ma.src.items;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,5 +1,6 @@
using Chickensoft.Introspection;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,6 +1,7 @@
using Chickensoft.Introspection;
using Chickensoft.Serialization;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -2,6 +2,7 @@
using Chickensoft.Collections;
using Chickensoft.SaveFileBuilder;
using Godot;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -7,6 +7,7 @@ using Godot;
using Godot.Collections;
using System;
using System.Linq;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;

View File

@@ -1,4 +1,6 @@
namespace Zennysoft.Game.Ma;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;
public interface IHasPrimaryAttack
{