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

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,67 +0,0 @@
using Godot;
using System.Text.Json.Serialization;
using System.Text.Json;
using System;
/// <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();
}
}

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

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

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
{