Skip to content

Commit

Permalink
Replication in AppDataFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Neakita committed Sep 6, 2024
1 parent 96b6f07 commit 092962d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion SightKeeper.Avalonia/Setup/ServicesBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions SightKeeper.Data.Tests/Binary/BinarySerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions SightKeeper.Data/Binary/AppData.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using SightKeeper.Application;
using SightKeeper.Domain.Model;
using SightKeeper.Domain.Model.DataSets;
using SightKeeper.Domain.Model.Profiles;

namespace SightKeeper.Data.Binary;

public sealed class AppData : ApplicationSettingsProvider
{
public HashSet<DataSet> DataSets { get; }
public HashSet<Game> Games { get; }
public HashSet<Profile> Profiles { get; }
public ApplicationSettings ApplicationSettings { get; }

public bool CustomDecorations
Expand All @@ -21,20 +19,17 @@ public bool CustomDecorations
internal AppData(
HashSet<Game> games,
HashSet<DataSet> dataSets,
HashSet<Profile> profiles,
ApplicationSettings applicationSettings)
{
DataSets = dataSets;
Games = games;
Profiles = profiles;
ApplicationSettings = applicationSettings;
}

internal AppData()
{
DataSets = new HashSet<DataSet>();
Games = new HashSet<Game>();
Profiles = new HashSet<Profile>();
ApplicationSettings = new ApplicationSettings();
}
}
14 changes: 10 additions & 4 deletions SightKeeper.Data/Binary/AppDataFormatter.cs
Original file line number Diff line number Diff line change
@@ -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<AppData>
{
public AppDataFormatter(
FileSystemScreenshotsDataAccess screenshotsDataAccess,
FileSystemWeightsDataAccess weightsDataAccess)
public AppDataFormatter(FileSystemScreenshotsDataAccess screenshotsDataAccess)
{
_dataSetConverter = new MultiDataSetConverter(screenshotsDataAccess);
_dataSetReplicator = new MultiDataSetReplicator(screenshotsDataAccess);
}

public override void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref AppData? value)
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<DataSet> Replicate(ImmutableArray<PackableDataSet> 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))
};
}
}

0 comments on commit 092962d

Please sign in to comment.