From 86cc6529660d40ecd31416554fdb3d5f055b9ad7 Mon Sep 17 00:00:00 2001 From: Steve Jackson Date: Thu, 23 May 2024 13:16:42 +0100 Subject: [PATCH] There is an issue where saving a profile causes duplicates in ManagerConfig.json This was caused by CharacterSummaryViewModel having the rememberLogin defaulting to true - if the profile was already saved the login box is not shown and this variable then remains true, which causes the code that checks to see if the connection to EFT was successful and rememberLogin was also true to add the profile to the config. Have also added a clean up routine in CharacterSelectionViewModel that removes all duplicate saved profiles (if there are any). It chooses the profile to keep by grouping by username and password, then selecting either the first entry with a ProfileID or the first entry if there are no Profile ID's. All profiles for that username/password are then removed, the profile to be kept placed back into the config and the configuration saved to disk. --- .../Play/CharacterSelectionViewModel.cs | 36 ++++++++++++++++--- .../Play/CharacterSummaryViewModel.cs | 5 ++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/SIT.Manager/ViewModels/Play/CharacterSelectionViewModel.cs b/SIT.Manager/ViewModels/Play/CharacterSelectionViewModel.cs index ae8e4aff..201e197f 100644 --- a/SIT.Manager/ViewModels/Play/CharacterSelectionViewModel.cs +++ b/SIT.Manager/ViewModels/Play/CharacterSelectionViewModel.cs @@ -4,6 +4,7 @@ using FluentAvalonia.UI.Controls; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using SharpCompress; using SIT.Manager.Interfaces; using SIT.Manager.Interfaces.ManagedProcesses; using SIT.Manager.Models.Aki; @@ -116,6 +117,11 @@ private async Task ReloadCharacterList() { _logger.LogError(ex, "An error occured while fetching characters"); } + + if (_connectedServer.Characters.Any()) + { + CheckAndRemoveDuplicateSavedProfiles(); + } } protected override async void OnActivated() @@ -123,10 +129,8 @@ protected override async void OnActivated() base.OnActivated(); AkiServer? currentServer = _configService.Config.BookmarkedServers.FirstOrDefault(x => x.Address == _connectedServer.Address); - if (currentServer != null) - { - _connectedServer = currentServer; - } + + if (currentServer != null) _connectedServer = currentServer; await ReloadCharacterList(); @@ -135,4 +139,28 @@ protected override async void OnActivated() ShowOnlySavedProfiles = true; } } + + private void CheckAndRemoveDuplicateSavedProfiles() + { + var characterGrouping = _connectedServer.Characters.GroupBy(x => new { x.Username, x.Password }); + + bool updateConfig = false; + + characterGrouping.Where(x => x.Count() > 1).ForEach(duplications => + { + // Get the first entry with a profile ID - or just the first entry if no saved profile ID. + AkiCharacter? duplicateToKeep = duplications.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.ProfileID)) ?? + duplications.First(); + + _connectedServer.Characters.RemoveAll(x => x.Username == duplicateToKeep.Username && x.Password == duplicateToKeep.Password); + _connectedServer.Characters.Add(duplicateToKeep); + + updateConfig = true; + }); + + if (updateConfig) + { + _configService.UpdateConfig(_configService.Config); + } + } } diff --git a/SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs b/SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs index bd7d0dc7..2d5f6425 100644 --- a/SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs +++ b/SIT.Manager/ViewModels/Play/CharacterSummaryViewModel.cs @@ -121,7 +121,10 @@ private async Task Play() } AkiCharacter? character = _connectedServer.Characters.FirstOrDefault(x => x.Username == Profile.Username); - bool rememberLogin = true; + + // Set this to false rather than true - this was causing duplicate saved profiles + // If we were already logged on the code to see if EFT was launched AND remember password would pass and save a duplicate each time + bool rememberLogin = false; if (character == null) {