From 092962d77c2049074e3bc699e6250d8a5f2e84a5 Mon Sep 17 00:00:00 2001 From: Katter Date: Fri, 6 Sep 2024 19:51:22 +0500 Subject: [PATCH] Replication in AppDataFormatter --- .../Setup/ServicesBootstrapper.cs | 2 +- .../Binary/BinarySerializationTests.cs | 3 +- SightKeeper.Data/Binary/AppData.cs | 5 --- SightKeeper.Data/Binary/AppDataFormatter.cs | 14 +++++-- .../DataSets/MultiDataSetReplicator.cs | 39 +++++++++++++++++++ 5 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 SightKeeper.Data/Binary/Replication/DataSets/MultiDataSetReplicator.cs diff --git a/SightKeeper.Avalonia/Setup/ServicesBootstrapper.cs b/SightKeeper.Avalonia/Setup/ServicesBootstrapper.cs index 1b6e2ae0..9980c6c2 100644 --- a/SightKeeper.Avalonia/Setup/ServicesBootstrapper.cs +++ b/SightKeeper.Avalonia/Setup/ServicesBootstrapper.cs @@ -33,7 +33,7 @@ private static void SetupBinarySerialization(ContainerBuilder builder) { FileSystemScreenshotsDataAccess screenshotsDataAccess = new(); FileSystemWeightsDataAccess weightsDataAccess = new(); - MemoryPackFormatterProvider.Register(new AppDataFormatter(screenshotsDataAccess, weightsDataAccess)); + MemoryPackFormatterProvider.Register(new AppDataFormatter(screenshotsDataAccess)); AppDataAccess appDataAccess = new(); appDataAccess.Load(); builder.RegisterInstance(screenshotsDataAccess); diff --git a/SightKeeper.Data.Tests/Binary/BinarySerializationTests.cs b/SightKeeper.Data.Tests/Binary/BinarySerializationTests.cs index 69dab9c1..82660e0a 100644 --- a/SightKeeper.Data.Tests/Binary/BinarySerializationTests.cs +++ b/SightKeeper.Data.Tests/Binary/BinarySerializationTests.cs @@ -18,8 +18,7 @@ public void ShouldSaveAndLoadAppData() { AppDataAccess dataAccess = new(); FileSystemScreenshotsDataAccess screenshotsDataAccess = new(); - FileSystemWeightsDataAccess weightsDataAccess = new(); - MemoryPackFormatterProvider.Register(new AppDataFormatter(screenshotsDataAccess, weightsDataAccess)); + MemoryPackFormatterProvider.Register(new AppDataFormatter(screenshotsDataAccess)); Game game = new("PayDay 2", "payday2"); dataAccess.Data.Games.Add(game); var dataSet = CreateDetectorDataSet(screenshotsDataAccess, game); diff --git a/SightKeeper.Data/Binary/AppData.cs b/SightKeeper.Data/Binary/AppData.cs index 8bec386b..92c128b3 100644 --- a/SightKeeper.Data/Binary/AppData.cs +++ b/SightKeeper.Data/Binary/AppData.cs @@ -1,7 +1,6 @@ using SightKeeper.Application; using SightKeeper.Domain.Model; using SightKeeper.Domain.Model.DataSets; -using SightKeeper.Domain.Model.Profiles; namespace SightKeeper.Data.Binary; @@ -9,7 +8,6 @@ public sealed class AppData : ApplicationSettingsProvider { public HashSet DataSets { get; } public HashSet Games { get; } - public HashSet Profiles { get; } public ApplicationSettings ApplicationSettings { get; } public bool CustomDecorations @@ -21,12 +19,10 @@ public bool CustomDecorations internal AppData( HashSet games, HashSet dataSets, - HashSet profiles, ApplicationSettings applicationSettings) { DataSets = dataSets; Games = games; - Profiles = profiles; ApplicationSettings = applicationSettings; } @@ -34,7 +30,6 @@ internal AppData() { DataSets = new HashSet(); Games = new HashSet(); - Profiles = new HashSet(); ApplicationSettings = new ApplicationSettings(); } } \ No newline at end of file diff --git a/SightKeeper.Data/Binary/AppDataFormatter.cs b/SightKeeper.Data/Binary/AppDataFormatter.cs index 2c8752cf..0b1e4252 100644 --- a/SightKeeper.Data/Binary/AppDataFormatter.cs +++ b/SightKeeper.Data/Binary/AppDataFormatter.cs @@ -1,17 +1,18 @@ using MemoryPack; using SightKeeper.Data.Binary.Conversion; using SightKeeper.Data.Binary.Conversion.DataSets; +using SightKeeper.Data.Binary.Replication; +using SightKeeper.Data.Binary.Replication.DataSets; using SightKeeper.Data.Binary.Services; namespace SightKeeper.Data.Binary; public sealed class AppDataFormatter : MemoryPackFormatter { - public AppDataFormatter( - FileSystemScreenshotsDataAccess screenshotsDataAccess, - FileSystemWeightsDataAccess weightsDataAccess) + public AppDataFormatter(FileSystemScreenshotsDataAccess screenshotsDataAccess) { _dataSetConverter = new MultiDataSetConverter(screenshotsDataAccess); + _dataSetReplicator = new MultiDataSetReplicator(screenshotsDataAccess); } public override void Serialize(ref MemoryPackWriter writer, scoped ref AppData? value) @@ -43,8 +44,13 @@ public override void Deserialize(ref MemoryPackReader reader, scoped ref AppData value = null; return; } - throw new NotImplementedException(); + ReplicationSession session = new(); + value = new AppData( + GameReplicator.Replicate(packable.Games, session), + _dataSetReplicator.Replicate(packable.DataSets, session), + packable.ApplicationSettings); } private readonly MultiDataSetConverter _dataSetConverter; + private readonly MultiDataSetReplicator _dataSetReplicator; } \ No newline at end of file diff --git a/SightKeeper.Data/Binary/Replication/DataSets/MultiDataSetReplicator.cs b/SightKeeper.Data/Binary/Replication/DataSets/MultiDataSetReplicator.cs new file mode 100644 index 00000000..86a96f73 --- /dev/null +++ b/SightKeeper.Data/Binary/Replication/DataSets/MultiDataSetReplicator.cs @@ -0,0 +1,39 @@ +using System.Collections.Immutable; +using SightKeeper.Data.Binary.Model.DataSets; +using SightKeeper.Data.Binary.Services; +using SightKeeper.Domain.Model.DataSets; + +namespace SightKeeper.Data.Binary.Replication.DataSets; + +internal sealed class MultiDataSetReplicator +{ + public MultiDataSetReplicator(FileSystemScreenshotsDataAccess screenshotsDataAccess) + { + _classifierReplicator = new ClassifierDataSetReplicator(screenshotsDataAccess); + _detectorReplicator = new DetectorDataSetReplicator(screenshotsDataAccess); + _poser2DReplicator = new Poser2DDataSetReplicator(screenshotsDataAccess); + _poser3DReplicator = new Poser3DDataSetReplicator(screenshotsDataAccess); + } + + public HashSet Replicate(ImmutableArray packableDataSets, ReplicationSession session) + { + return packableDataSets.Select(dataSet => Replicate(dataSet, session)).ToHashSet(); + } + + private readonly ClassifierDataSetReplicator _classifierReplicator; + private readonly DetectorDataSetReplicator _detectorReplicator; + private readonly Poser2DDataSetReplicator _poser2DReplicator; + private readonly Poser3DDataSetReplicator _poser3DReplicator; + + private DataSet Replicate(PackableDataSet packableDataSet, ReplicationSession session) + { + return packableDataSet switch + { + PackableClassifierDataSet classifierDataSet => _classifierReplicator.Replicate(classifierDataSet, session), + PackableDetectorDataSet detectorDataSet => _detectorReplicator.Replicate(detectorDataSet, session), + PackablePoser2DDataSet poser2DDataSet => _poser2DReplicator.Replicate(poser2DDataSet, session), + PackablePoser3DDataSet poser3DDataSet => _poser3DReplicator.Replicate(poser3DDataSet, session), + _ => throw new ArgumentOutOfRangeException(nameof(packableDataSet)) + }; + } +} \ No newline at end of file