From 3b5ee84ce26067b4efe6f9f6ae0653d5e870d06c Mon Sep 17 00:00:00 2001 From: Zenny Date: Fri, 7 Mar 2025 00:32:19 -0800 Subject: [PATCH] Move save logic out of Game --- Ma.sln | 14 ++++- .../Save/ISaveFileManager.cs | 10 ++++ .../Zennysoft.Game.Abstractions.csproj | 9 +++ Zennysoft.Game.Ma.Implementation/Class1.cs | 6 -- .../EnumTypes.cs | 45 ++++++++++++++- .../Save/SaveFileManager.cs | 56 +++++++++++++++++++ .../Texture2DConverter.cs | 3 +- .../Zennysoft.Game.Ma.Implementation.csproj | 27 +++++++-- .../{Zennysoft.Game.Ma.csproj => Ma.csproj} | 10 +++- Zennysoft.Game.Ma/src/Main.cs | 6 -- Zennysoft.Game.Ma/src/boss/Boss.cs | 1 + Zennysoft.Game.Ma/src/enemy/Enemy.cs | 1 + Zennysoft.Game.Ma/src/enemy/IEnemy.cs | 1 + Zennysoft.Game.Ma/src/enemy/WeaponTag.cs | 23 -------- .../enemy_types/01. sproingy/Sproingy.cs | 1 + .../enemy/enemy_types/02. michael/Michael.cs | 1 + .../enemy_types/03. filth_eater/FilthEater.cs | 1 + .../enemy/enemy_types/05. ballos/Ballos.cs | 1 + .../enemy/enemy_types/06. chariot/Chariot.cs | 1 + .../enemy/enemy_types/07. chinthe/Chinthe.cs | 1 + .../enemy_types/10. Eden Pillar/EdenPillar.cs | 1 + Zennysoft.Game.Ma/src/game/ElementType.cs | 16 ------ Zennysoft.Game.Ma/src/game/Game.cs | 56 +++++++------------ Zennysoft.Game.Ma/src/items/EffectService.cs | 1 + Zennysoft.Game.Ma/src/items/EquipableItem.cs | 1 + .../src/items/InventoryItemStats.cs | 1 + .../src/items/accessory/Accessory.cs | 1 + .../src/items/accessory/AccessoryStats.cs | 13 +---- Zennysoft.Game.Ma/src/items/armor/Armor.cs | 1 + .../src/items/effect/EffectItem.cs | 1 + .../src/items/effect/EffectItemStats.cs | 1 + .../src/items/throwable/ThrowableItem.cs | 1 + .../src/items/throwable/ThrowableItemStats.cs | 1 + .../src/items/thrown/ThrownItem.cs | 1 + Zennysoft.Game.Ma/src/items/weapons/Weapon.cs | 1 + .../src/items/weapons/WeaponStats.cs | 1 + Zennysoft.Game.Ma/src/player/IPlayer.cs | 1 + Zennysoft.Game.Ma/src/player/Player.cs | 1 + .../src/system/IHasPrimaryAttack.cs | 4 +- 39 files changed, 212 insertions(+), 110 deletions(-) create mode 100644 Zennysoft.Game.Abstractions/Save/ISaveFileManager.cs create mode 100644 Zennysoft.Game.Abstractions/Zennysoft.Game.Abstractions.csproj delete mode 100644 Zennysoft.Game.Ma.Implementation/Class1.cs rename Zennysoft.Game.Ma/src/items/throwable/ThrowableItemTag.cs => Zennysoft.Game.Ma.Implementation/EnumTypes.cs (54%) create mode 100644 Zennysoft.Game.Ma.Implementation/Save/SaveFileManager.cs rename Zennysoft.Game.Ma/src/items/InventoryItemTextureExtensions.cs => Zennysoft.Game.Ma.Implementation/Texture2DConverter.cs (98%) rename Zennysoft.Game.Ma/{Zennysoft.Game.Ma.csproj => Ma.csproj} (80%) delete mode 100644 Zennysoft.Game.Ma/src/enemy/WeaponTag.cs delete mode 100644 Zennysoft.Game.Ma/src/game/ElementType.cs diff --git a/Ma.sln b/Ma.sln index 3270a382..f4686cbd 100644 --- a/Ma.sln +++ b/Ma.sln @@ -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 diff --git a/Zennysoft.Game.Abstractions/Save/ISaveFileManager.cs b/Zennysoft.Game.Abstractions/Save/ISaveFileManager.cs new file mode 100644 index 00000000..65e235b6 --- /dev/null +++ b/Zennysoft.Game.Abstractions/Save/ISaveFileManager.cs @@ -0,0 +1,10 @@ +using System.Text.Json; + +namespace Zennysoft.Game.Abstractions; + +public interface ISaveFileManager +{ + public Task WriteToFile(T gameData); + + public Task ReadFromFile(); +} diff --git a/Zennysoft.Game.Abstractions/Zennysoft.Game.Abstractions.csproj b/Zennysoft.Game.Abstractions/Zennysoft.Game.Abstractions.csproj new file mode 100644 index 00000000..fa71b7ae --- /dev/null +++ b/Zennysoft.Game.Abstractions/Zennysoft.Game.Abstractions.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/Zennysoft.Game.Ma.Implementation/Class1.cs b/Zennysoft.Game.Ma.Implementation/Class1.cs deleted file mode 100644 index 5c01a717..00000000 --- a/Zennysoft.Game.Ma.Implementation/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Zennysoft.Game.Ma.Implementation; - -public class Class1 -{ - -} diff --git a/Zennysoft.Game.Ma/src/items/throwable/ThrowableItemTag.cs b/Zennysoft.Game.Ma.Implementation/EnumTypes.cs similarity index 54% rename from Zennysoft.Game.Ma/src/items/throwable/ThrowableItemTag.cs rename to Zennysoft.Game.Ma.Implementation/EnumTypes.cs index fd5743ea..78b04e56 100644 --- a/Zennysoft.Game.Ma/src/items/throwable/ThrowableItemTag.cs +++ b/Zennysoft.Game.Ma.Implementation/EnumTypes.cs @@ -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; diff --git a/Zennysoft.Game.Ma.Implementation/Save/SaveFileManager.cs b/Zennysoft.Game.Ma.Implementation/Save/SaveFileManager.cs new file mode 100644 index 00000000..adcfdd87 --- /dev/null +++ b/Zennysoft.Game.Ma.Implementation/Save/SaveFileManager.cs @@ -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 : ISaveFileManager +{ + 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 ReadFromFile() + { + if (!_fileSystem.File.Exists(SaveFilePath)) + throw new FileNotFoundException(); + + var json = await _fileSystem.File.ReadAllTextAsync(SaveFilePath); + return JsonSerializer.Deserialize(json, JsonOptions); + } + + public async Task WriteToFile(T gameData) + { + var json = JsonSerializer.Serialize(gameData, JsonOptions); + await _fileSystem.File.WriteAllTextAsync(SaveFilePath, json); + } +} diff --git a/Zennysoft.Game.Ma/src/items/InventoryItemTextureExtensions.cs b/Zennysoft.Game.Ma.Implementation/Texture2DConverter.cs similarity index 98% rename from Zennysoft.Game.Ma/src/items/InventoryItemTextureExtensions.cs rename to Zennysoft.Game.Ma.Implementation/Texture2DConverter.cs index 08e21d77..79bf62f9 100644 --- a/Zennysoft.Game.Ma/src/items/InventoryItemTextureExtensions.cs +++ b/Zennysoft.Game.Ma.Implementation/Texture2DConverter.cs @@ -1,8 +1,7 @@ -using Godot; +using Godot; using System.Text.Json.Serialization; using System.Text.Json; -using System; /// Basis JSON converter. public class Texture2DConverter : JsonConverter diff --git a/Zennysoft.Game.Ma.Implementation/Zennysoft.Game.Ma.Implementation.csproj b/Zennysoft.Game.Ma.Implementation/Zennysoft.Game.Ma.Implementation.csproj index fa71b7ae..c44c8107 100644 --- a/Zennysoft.Game.Ma.Implementation/Zennysoft.Game.Ma.Implementation.csproj +++ b/Zennysoft.Game.Ma.Implementation/Zennysoft.Game.Ma.Implementation.csproj @@ -1,9 +1,26 @@  - - net8.0 - enable - enable - + + net8.0 + enable + enable + + + + + + + + + + + + + + + + + + diff --git a/Zennysoft.Game.Ma/Zennysoft.Game.Ma.csproj b/Zennysoft.Game.Ma/Ma.csproj similarity index 80% rename from Zennysoft.Game.Ma/Zennysoft.Game.Ma.csproj rename to Zennysoft.Game.Ma/Ma.csproj index 8fad3c53..a4da56dc 100644 --- a/Zennysoft.Game.Ma/Zennysoft.Game.Ma.csproj +++ b/Zennysoft.Game.Ma/Ma.csproj @@ -5,6 +5,10 @@ true + + + + @@ -20,17 +24,21 @@ + - + + + + diff --git a/Zennysoft.Game.Ma/src/Main.cs b/Zennysoft.Game.Ma/src/Main.cs index a4b1d6f8..ded8a27b 100644 --- a/Zennysoft.Game.Ma/src/Main.cs +++ b/Zennysoft.Game.Ma/src/Main.cs @@ -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. diff --git a/Zennysoft.Game.Ma/src/boss/Boss.cs b/Zennysoft.Game.Ma/src/boss/Boss.cs index d5498ef6..ac534d00 100644 --- a/Zennysoft.Game.Ma/src/boss/Boss.cs +++ b/Zennysoft.Game.Ma/src/boss/Boss.cs @@ -3,6 +3,7 @@ using Chickensoft.Collections; using Chickensoft.GodotNodeInterfaces; using Chickensoft.Introspection; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/enemy/Enemy.cs b/Zennysoft.Game.Ma/src/enemy/Enemy.cs index c87e7513..c2e8e72e 100644 --- a/Zennysoft.Game.Ma/src/enemy/Enemy.cs +++ b/Zennysoft.Game.Ma/src/enemy/Enemy.cs @@ -3,6 +3,7 @@ using Chickensoft.Collections; using Chickensoft.Introspection; using Godot; using System.Linq; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/enemy/IEnemy.cs b/Zennysoft.Game.Ma/src/enemy/IEnemy.cs index 95a19f9f..c52be49c 100644 --- a/Zennysoft.Game.Ma/src/enemy/IEnemy.cs +++ b/Zennysoft.Game.Ma/src/enemy/IEnemy.cs @@ -1,4 +1,5 @@ using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/enemy/WeaponTag.cs b/Zennysoft.Game.Ma/src/enemy/WeaponTag.cs deleted file mode 100644 index 2b8315cd..00000000 --- a/Zennysoft.Game.Ma/src/enemy/WeaponTag.cs +++ /dev/null @@ -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; diff --git a/Zennysoft.Game.Ma/src/enemy/enemy_types/01. sproingy/Sproingy.cs b/Zennysoft.Game.Ma/src/enemy/enemy_types/01. sproingy/Sproingy.cs index f1e7eb81..11dee248 100644 --- a/Zennysoft.Game.Ma/src/enemy/enemy_types/01. sproingy/Sproingy.cs +++ b/Zennysoft.Game.Ma/src/enemy/enemy_types/01. sproingy/Sproingy.cs @@ -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; diff --git a/Zennysoft.Game.Ma/src/enemy/enemy_types/02. michael/Michael.cs b/Zennysoft.Game.Ma/src/enemy/enemy_types/02. michael/Michael.cs index ce71acfc..64c1308c 100644 --- a/Zennysoft.Game.Ma/src/enemy/enemy_types/02. michael/Michael.cs +++ b/Zennysoft.Game.Ma/src/enemy/enemy_types/02. michael/Michael.cs @@ -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; diff --git a/Zennysoft.Game.Ma/src/enemy/enemy_types/03. filth_eater/FilthEater.cs b/Zennysoft.Game.Ma/src/enemy/enemy_types/03. filth_eater/FilthEater.cs index fc6b6224..e11b260a 100644 --- a/Zennysoft.Game.Ma/src/enemy/enemy_types/03. filth_eater/FilthEater.cs +++ b/Zennysoft.Game.Ma/src/enemy/enemy_types/03. filth_eater/FilthEater.cs @@ -3,6 +3,7 @@ using Chickensoft.Introspection; using Godot; using System; using System.Collections.Generic; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/enemy/enemy_types/05. ballos/Ballos.cs b/Zennysoft.Game.Ma/src/enemy/enemy_types/05. ballos/Ballos.cs index 6b77e614..90d516c2 100644 --- a/Zennysoft.Game.Ma/src/enemy/enemy_types/05. ballos/Ballos.cs +++ b/Zennysoft.Game.Ma/src/enemy/enemy_types/05. ballos/Ballos.cs @@ -3,6 +3,7 @@ using Chickensoft.Introspection; using Godot; using System.Collections.Generic; using System; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/enemy/enemy_types/06. chariot/Chariot.cs b/Zennysoft.Game.Ma/src/enemy/enemy_types/06. chariot/Chariot.cs index 66609d2d..1699b8fd 100644 --- a/Zennysoft.Game.Ma/src/enemy/enemy_types/06. chariot/Chariot.cs +++ b/Zennysoft.Game.Ma/src/enemy/enemy_types/06. chariot/Chariot.cs @@ -1,6 +1,7 @@ using Chickensoft.AutoInject; using Chickensoft.Introspection; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/enemy/enemy_types/07. chinthe/Chinthe.cs b/Zennysoft.Game.Ma/src/enemy/enemy_types/07. chinthe/Chinthe.cs index df376bb6..19848318 100644 --- a/Zennysoft.Game.Ma/src/enemy/enemy_types/07. chinthe/Chinthe.cs +++ b/Zennysoft.Game.Ma/src/enemy/enemy_types/07. chinthe/Chinthe.cs @@ -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; diff --git a/Zennysoft.Game.Ma/src/enemy/enemy_types/10. Eden Pillar/EdenPillar.cs b/Zennysoft.Game.Ma/src/enemy/enemy_types/10. Eden Pillar/EdenPillar.cs index 34c67621..23fc1fc1 100644 --- a/Zennysoft.Game.Ma/src/enemy/enemy_types/10. Eden Pillar/EdenPillar.cs +++ b/Zennysoft.Game.Ma/src/enemy/enemy_types/10. Eden Pillar/EdenPillar.cs @@ -3,6 +3,7 @@ using Chickensoft.Introspection; using Godot; using System.Collections.Generic; using System; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/game/ElementType.cs b/Zennysoft.Game.Ma/src/game/ElementType.cs deleted file mode 100644 index 72992efd..00000000 --- a/Zennysoft.Game.Ma/src/game/ElementType.cs +++ /dev/null @@ -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; diff --git a/Zennysoft.Game.Ma/src/game/Game.cs b/Zennysoft.Game.Ma/src/game/Game.cs index 90313c4d..21fadac6 100644 --- a/Zennysoft.Game.Ma/src/game/Game.cs +++ b/Zennysoft.Game.Ma/src/game/Game.cs @@ -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.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 SaveFile { get; set; } = default!; public ISaveChunk 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(); + _container.Register, SaveFileManager>(); + _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( (chunk) => { @@ -162,25 +148,23 @@ public partial class Game : Node3D, IGame public void OnResolved() { + var saveFileManager = _container.GetInstance>(); SaveFile = new SaveFile( 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(json, JsonOptions); + return null; } ); diff --git a/Zennysoft.Game.Ma/src/items/EffectService.cs b/Zennysoft.Game.Ma/src/items/EffectService.cs index f3715747..0f2dff04 100644 --- a/Zennysoft.Game.Ma/src/items/EffectService.cs +++ b/Zennysoft.Game.Ma/src/items/EffectService.cs @@ -1,6 +1,7 @@ using Godot; using System.Linq; using System; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma.src.items; diff --git a/Zennysoft.Game.Ma/src/items/EquipableItem.cs b/Zennysoft.Game.Ma/src/items/EquipableItem.cs index 9f2ef66f..ceb55594 100644 --- a/Zennysoft.Game.Ma/src/items/EquipableItem.cs +++ b/Zennysoft.Game.Ma/src/items/EquipableItem.cs @@ -1,5 +1,6 @@ using Chickensoft.Introspection; using Chickensoft.Serialization; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/items/InventoryItemStats.cs b/Zennysoft.Game.Ma/src/items/InventoryItemStats.cs index 76937cf7..9707eee6 100644 --- a/Zennysoft.Game.Ma/src/items/InventoryItemStats.cs +++ b/Zennysoft.Game.Ma/src/items/InventoryItemStats.cs @@ -1,6 +1,7 @@ using Chickensoft.Introspection; using Chickensoft.Serialization; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/items/accessory/Accessory.cs b/Zennysoft.Game.Ma/src/items/accessory/Accessory.cs index 1f91ec9b..ede6ed29 100644 --- a/Zennysoft.Game.Ma/src/items/accessory/Accessory.cs +++ b/Zennysoft.Game.Ma/src/items/accessory/Accessory.cs @@ -1,5 +1,6 @@ using Chickensoft.Introspection; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/items/accessory/AccessoryStats.cs b/Zennysoft.Game.Ma/src/items/accessory/AccessoryStats.cs index 0da35091..9195973e 100644 --- a/Zennysoft.Game.Ma/src/items/accessory/AccessoryStats.cs +++ b/Zennysoft.Game.Ma/src/items/accessory/AccessoryStats.cs @@ -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; +} \ No newline at end of file diff --git a/Zennysoft.Game.Ma/src/items/armor/Armor.cs b/Zennysoft.Game.Ma/src/items/armor/Armor.cs index 8b505ee3..e5e2edbb 100644 --- a/Zennysoft.Game.Ma/src/items/armor/Armor.cs +++ b/Zennysoft.Game.Ma/src/items/armor/Armor.cs @@ -1,5 +1,6 @@ using Chickensoft.Introspection; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/items/effect/EffectItem.cs b/Zennysoft.Game.Ma/src/items/effect/EffectItem.cs index af04f927..3a191d8f 100644 --- a/Zennysoft.Game.Ma/src/items/effect/EffectItem.cs +++ b/Zennysoft.Game.Ma/src/items/effect/EffectItem.cs @@ -1,5 +1,6 @@ using Chickensoft.Introspection; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/items/effect/EffectItemStats.cs b/Zennysoft.Game.Ma/src/items/effect/EffectItemStats.cs index b2ef8eff..ae7b2961 100644 --- a/Zennysoft.Game.Ma/src/items/effect/EffectItemStats.cs +++ b/Zennysoft.Game.Ma/src/items/effect/EffectItemStats.cs @@ -1,6 +1,7 @@ using Chickensoft.Introspection; using Chickensoft.Serialization; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/items/throwable/ThrowableItem.cs b/Zennysoft.Game.Ma/src/items/throwable/ThrowableItem.cs index a4ff0566..6ee71c5c 100644 --- a/Zennysoft.Game.Ma/src/items/throwable/ThrowableItem.cs +++ b/Zennysoft.Game.Ma/src/items/throwable/ThrowableItem.cs @@ -1,5 +1,6 @@ using Chickensoft.Introspection; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/items/throwable/ThrowableItemStats.cs b/Zennysoft.Game.Ma/src/items/throwable/ThrowableItemStats.cs index e92b82b1..074bc971 100644 --- a/Zennysoft.Game.Ma/src/items/throwable/ThrowableItemStats.cs +++ b/Zennysoft.Game.Ma/src/items/throwable/ThrowableItemStats.cs @@ -1,6 +1,7 @@ using Chickensoft.Introspection; using Chickensoft.Serialization; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/items/thrown/ThrownItem.cs b/Zennysoft.Game.Ma/src/items/thrown/ThrownItem.cs index 448f66cf..64155e1f 100644 --- a/Zennysoft.Game.Ma/src/items/thrown/ThrownItem.cs +++ b/Zennysoft.Game.Ma/src/items/thrown/ThrownItem.cs @@ -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; diff --git a/Zennysoft.Game.Ma/src/items/weapons/Weapon.cs b/Zennysoft.Game.Ma/src/items/weapons/Weapon.cs index f09320a8..65882332 100644 --- a/Zennysoft.Game.Ma/src/items/weapons/Weapon.cs +++ b/Zennysoft.Game.Ma/src/items/weapons/Weapon.cs @@ -1,5 +1,6 @@ using Chickensoft.Introspection; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/items/weapons/WeaponStats.cs b/Zennysoft.Game.Ma/src/items/weapons/WeaponStats.cs index 7406c5c6..08f83b55 100644 --- a/Zennysoft.Game.Ma/src/items/weapons/WeaponStats.cs +++ b/Zennysoft.Game.Ma/src/items/weapons/WeaponStats.cs @@ -1,6 +1,7 @@ using Chickensoft.Introspection; using Chickensoft.Serialization; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/player/IPlayer.cs b/Zennysoft.Game.Ma/src/player/IPlayer.cs index 12567b6e..070d33b9 100644 --- a/Zennysoft.Game.Ma/src/player/IPlayer.cs +++ b/Zennysoft.Game.Ma/src/player/IPlayer.cs @@ -2,6 +2,7 @@ using Chickensoft.Collections; using Chickensoft.SaveFileBuilder; using Godot; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/player/Player.cs b/Zennysoft.Game.Ma/src/player/Player.cs index fd63550b..3ceb5c07 100644 --- a/Zennysoft.Game.Ma/src/player/Player.cs +++ b/Zennysoft.Game.Ma/src/player/Player.cs @@ -7,6 +7,7 @@ using Godot; using Godot.Collections; using System; using System.Linq; +using Zennysoft.Game.Ma.Implementation; namespace Zennysoft.Game.Ma; diff --git a/Zennysoft.Game.Ma/src/system/IHasPrimaryAttack.cs b/Zennysoft.Game.Ma/src/system/IHasPrimaryAttack.cs index 6079ce19..36845b8d 100644 --- a/Zennysoft.Game.Ma/src/system/IHasPrimaryAttack.cs +++ b/Zennysoft.Game.Ma/src/system/IHasPrimaryAttack.cs @@ -1,4 +1,6 @@ -namespace Zennysoft.Game.Ma; +using Zennysoft.Game.Ma.Implementation; + +namespace Zennysoft.Game.Ma; public interface IHasPrimaryAttack {