diff --git a/MareSynchronos/MareConfiguration/ConfigurationMigrator.cs b/MareSynchronos/MareConfiguration/ConfigurationMigrator.cs index 14db85c..e70744c 100644 --- a/MareSynchronos/MareConfiguration/ConfigurationMigrator.cs +++ b/MareSynchronos/MareConfiguration/ConfigurationMigrator.cs @@ -47,6 +47,23 @@ public class ConfigurationMigrator : IHostedService _logger.LogWarning("Failed to migrate, skipping", ex); } } + + if (File.Exists(ConfigurationPath(ServerConfigService.ConfigName))) + { + try + { + var serverConfig = JsonConvert.DeserializeObject(File.ReadAllText(ConfigurationPath(ServerConfigService.ConfigName)))!; + + if (serverConfig.Version == 0) + { + MigrateServerConfigV0toV1(serverConfig); + } + } + catch (Exception ex) + { + _logger.LogWarning("Failed to migrate ServerConfig", ex); + } + } } public Task StartAsync(CancellationToken cancellationToken) @@ -75,11 +92,12 @@ public class ConfigurationMigrator : IHostedService MareConfig mareConfigV1 = mareConfigV0.ToV1(); + int i = 0; var serverConfig = new ServerConfig() { - CurrentServer = mareConfigV0.CurrentServer, - ServerStorage = mareConfigV0.ServerStorage.ToDictionary(p => p.Key, p => p.Value.ToV1(), StringComparer.Ordinal) + ServerStorage = mareConfigV0.ServerStorage.Select(p => p.Value.ToV1()).ToList() }; + serverConfig.CurrentServer = Array.IndexOf(serverConfig.ServerStorage.Select(s => s.ServerUri).ToArray(), mareConfigV0.CurrentServer); var transientConfig = new TransientConfig() { PlayerPersistentTransientCache = mareConfigV0.PlayerPersistentTransientCache @@ -108,6 +126,28 @@ public class ConfigurationMigrator : IHostedService SaveConfig(tagConfig, ConfigurationPath(ServerTagConfigService.ConfigName)); SaveConfig(notesConfig, ConfigurationPath(NotesConfigService.ConfigName)); } + + private void MigrateServerConfigV0toV1(ServerConfigV0 serverConfigV0) + { + _logger.LogInformation("Migration Server Configuration from version 0 to 1"); + if (File.Exists(ConfigurationPath(ServerConfigService.ConfigName))) + File.Copy(ConfigurationPath(ServerConfigService.ConfigName), ConfigurationPath(ServerConfigService.ConfigName) + ".migrated." + serverConfigV0.Version + ".bak", overwrite: true); + + ServerConfig migrated = new(); + + var currentServer = serverConfigV0.CurrentServer; + var currentServerIdx = Array.IndexOf(serverConfigV0.ServerStorage.Keys.ToArray(), currentServer); + + migrated.CurrentServer = currentServerIdx; + migrated.ServerStorage = new(); + + foreach (var server in serverConfigV0.ServerStorage) + { + migrated.ServerStorage.Add(server.Value); + } + + SaveConfig(migrated, ConfigurationPath(ServerConfigService.ConfigName)); + } } #pragma warning restore CS0618 // ignore Obsolete tag, the point of this migrator is to migrate obsolete configs to new ones \ No newline at end of file diff --git a/MareSynchronos/MareConfiguration/Configurations/IMareConfiguration.cs b/MareSynchronos/MareConfiguration/Configurations/IMareConfiguration.cs index 280f4e4..f988957 100644 --- a/MareSynchronos/MareConfiguration/Configurations/IMareConfiguration.cs +++ b/MareSynchronos/MareConfiguration/Configurations/IMareConfiguration.cs @@ -3,4 +3,4 @@ public interface IMareConfiguration { int Version { get; set; } -} +} \ No newline at end of file diff --git a/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs b/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs index cc30cf4..4ae0dd4 100644 --- a/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs +++ b/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs @@ -31,6 +31,9 @@ public class MareConfig : IMareConfiguration public bool ShowUploadingBigText { get; set; } = true; public bool ShowVisibleUsersSeparately { get; set; } = true; public int TimeSpanBetweenScansInSeconds { get; set; } = 30; + public int TransferBarsHeight { get; set; } = 12; + public bool TransferBarsShowText { get; set; } = true; + public int TransferBarsWidth { get; set; } = 250; public int Version { get; set; } = 1; public NotificationLocation WarningNotification { get; set; } = NotificationLocation.Both; } \ No newline at end of file diff --git a/MareSynchronos/MareConfiguration/Configurations/Obsolete/ServerConfigV0.cs b/MareSynchronos/MareConfiguration/Configurations/Obsolete/ServerConfigV0.cs new file mode 100644 index 0000000..5cd6f04 --- /dev/null +++ b/MareSynchronos/MareConfiguration/Configurations/Obsolete/ServerConfigV0.cs @@ -0,0 +1,18 @@ +using MareSynchronos.MareConfiguration.Models; +using MareSynchronos.WebAPI; + +namespace MareSynchronos.MareConfiguration.Configurations.Obsolete; + +[Serializable] +[Obsolete] +public class ServerConfigV0 : IMareConfiguration +{ + public string CurrentServer { get; set; } = string.Empty; + + public Dictionary ServerStorage { get; set; } = new(StringComparer.OrdinalIgnoreCase) + { + { ApiController.MainServiceUri, new ServerStorage() { ServerName = ApiController.MainServer, ServerUri = ApiController.MainServiceUri } }, + }; + + public int Version { get; set; } = 0; +} diff --git a/MareSynchronos/MareConfiguration/Configurations/ServerConfig.cs b/MareSynchronos/MareConfiguration/Configurations/ServerConfig.cs index 20262db..103eb20 100644 --- a/MareSynchronos/MareConfiguration/Configurations/ServerConfig.cs +++ b/MareSynchronos/MareConfiguration/Configurations/ServerConfig.cs @@ -6,11 +6,12 @@ namespace MareSynchronos.MareConfiguration.Configurations; [Serializable] public class ServerConfig : IMareConfiguration { - public int Version { get; set; } = 0; + public int CurrentServer { get; set; } = 0; - public Dictionary ServerStorage { get; set; } = new(StringComparer.OrdinalIgnoreCase) + public List ServerStorage { get; set; } = new() { - { ApiController.MainServiceUri, new ServerStorage() { ServerName = ApiController.MainServer, ServerUri = ApiController.MainServiceUri } }, + { new ServerStorage() { ServerName = ApiController.MainServer, ServerUri = ApiController.MainServiceUri } }, }; - public string CurrentServer { get; set; } = string.Empty; -} + + public int Version { get; set; } = 1; +} \ No newline at end of file diff --git a/MareSynchronos/MarePlugin.cs b/MareSynchronos/MarePlugin.cs index fee157e..ff3071e 100644 --- a/MareSynchronos/MarePlugin.cs +++ b/MareSynchronos/MarePlugin.cs @@ -77,6 +77,7 @@ public class MarePlugin : MediatorSubscriberBase, IHostedService public MarePlugin(ILogger logger, MareConfigService mareConfigService, ServerConfigurationManager serverConfigurationManager, DalamudUtilService dalamudUtil, + ConfigurationMigrator configurationMigrator, IServiceScopeFactory serviceScopeFactory, MareMediator mediator) : base(logger, mediator) { _mareConfigService = mareConfigService; diff --git a/MareSynchronos/MareSynchronos.csproj b/MareSynchronos/MareSynchronos.csproj index 74f4686..bf86660 100644 --- a/MareSynchronos/MareSynchronos.csproj +++ b/MareSynchronos/MareSynchronos.csproj @@ -3,7 +3,7 @@ - 0.8.6 + 0.8.7 https://github.com/Penumbra-Sync/client diff --git a/MareSynchronos/PlayerData/Pairs/CachedPlayer.cs b/MareSynchronos/PlayerData/Pairs/CachedPlayer.cs index eb8dc7c..85ffca9 100644 --- a/MareSynchronos/PlayerData/Pairs/CachedPlayer.cs +++ b/MareSynchronos/PlayerData/Pairs/CachedPlayer.cs @@ -158,7 +158,7 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase _charaHandler?.Dispose(); _charaHandler = null; - if (!_lifetime.ApplicationStopping.IsCancellationRequested && !_dalamudUtil.IsZoning) + if (!_lifetime.ApplicationStopping.IsCancellationRequested && !_dalamudUtil.IsZoning && !_dalamudUtil.IsInCutscene) { Logger.LogTrace("[{applicationId}] Restoring state for {name} ({OnlineUser})", applicationId, name, OnlineUser); _ipcManager.PenumbraRemoveTemporaryCollection(Logger, applicationId, name); @@ -258,6 +258,14 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase } } + private void CheckForNameAndThrow(GameObjectHandler handler, string name) + { + if (!string.Equals(handler.Name, name, StringComparison.OrdinalIgnoreCase)) + { + throw new InvalidOperationException("Player name not equal to requested name, pointer invalid"); + } + } + private Dictionary> CheckUpdatedData(CharacterData oldData, CharacterData newData, bool forced) { var charaDataToUpdate = new Dictionary>(); @@ -480,14 +488,6 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase } } - private void CheckForNameAndThrow(GameObjectHandler handler, string name) - { - if (!string.Equals(handler.Name, name, StringComparison.OrdinalIgnoreCase)) - { - throw new InvalidOperationException("Player name not equal to requested name, pointer invalid"); - } - } - private async Task RevertCustomizationData(ObjectKind objectKind, string name, Guid applicationId) { nint address = _dalamudUtil.GetPlayerCharacterFromObjectTableByName(name)?.Address ?? IntPtr.Zero; diff --git a/MareSynchronos/Plugin.cs b/MareSynchronos/Plugin.cs index bed06c7..4e26274 100644 --- a/MareSynchronos/Plugin.cs +++ b/MareSynchronos/Plugin.cs @@ -131,8 +131,8 @@ public sealed class Plugin : IDalamudPlugin s.GetRequiredService(), s.GetRequiredService(), s.GetRequiredService(), s.GetRequiredService(), pluginInterface, s.GetRequiredService(), s.GetRequiredService(), s.GetRequiredService())); - collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); + collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); }) diff --git a/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs b/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs index 1b980be..e34f91f 100644 --- a/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs +++ b/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs @@ -25,13 +25,9 @@ public class ServerConfigurationManager _dalamudUtil = dalamudUtil; } - public string CurrentApiUrl => string.IsNullOrEmpty(_configService.Current.CurrentServer) ? ApiController.MainServiceUri : _configService.Current.CurrentServer; - public ServerStorage? CurrentServer => _configService.Current.ServerStorage.TryGetValue(CurrentApiUrl, out ServerStorage? value) ? value : null; - - public int GetCurrentServerIndex() - { - return Array.IndexOf(_configService.Current.ServerStorage.Keys.ToArray(), CurrentApiUrl); - } + public string CurrentApiUrl => CurrentServer.ServerUri; + public ServerStorage CurrentServer => _configService.Current.ServerStorage[CurrentServerIndex]; + public int CurrentServerIndex => _configService.Current.CurrentServer; public string? GetSecretKey(int serverIdx = -1) { @@ -70,21 +66,21 @@ public class ServerConfigurationManager public string[] GetServerApiUrls() { - return _configService.Current.ServerStorage.Keys.ToArray(); + return _configService.Current.ServerStorage.Select(v => v.ServerUri).ToArray(); } public ServerStorage GetServerByIndex(int idx) { try { - return _configService.Current.ServerStorage.ElementAt(idx).Value; + return _configService.Current.ServerStorage.ElementAt(idx); } catch { - _configService.Current.CurrentServer = ApiController.MainServiceUri; - if (!_configService.Current.ServerStorage.ContainsKey(ApiController.MainServer)) + _configService.Current.CurrentServer = 0; + if (!string.Equals(_configService.Current.ServerStorage[0].ServerUri, ApiController.MainServer, StringComparison.OrdinalIgnoreCase)) { - _configService.Current.ServerStorage.Add(_configService.Current.CurrentServer, new ServerStorage() { ServerUri = ApiController.MainServiceUri, ServerName = ApiController.MainServer }); + _configService.Current.ServerStorage.Insert(0, new ServerStorage() { ServerUri = ApiController.MainServiceUri, ServerName = ApiController.MainServer }); } Save(); return CurrentServer!; @@ -93,7 +89,7 @@ public class ServerConfigurationManager public string[] GetServerNames() { - return _configService.Current.ServerStorage.Values.Select(v => v.ServerName).ToArray(); + return _configService.Current.ServerStorage.Select(v => v.ServerName).ToArray(); } public string? GetToken() @@ -133,14 +129,14 @@ public class ServerConfigurationManager public void SelectServer(int idx) { - _configService.Current.CurrentServer = GetServerByIndex(idx).ServerUri; + _configService.Current.CurrentServer = idx; CurrentServer!.FullPause = false; Save(); } internal void AddCurrentCharacterToServer(int serverSelectionIndex = -1, bool addLastSecretKey = false) { - if (serverSelectionIndex == -1) serverSelectionIndex = GetCurrentServerIndex(); + if (serverSelectionIndex == -1) serverSelectionIndex = CurrentServerIndex; var server = GetServerByIndex(serverSelectionIndex); server.Authentications.Add(new Authentication() { @@ -166,7 +162,7 @@ public class ServerConfigurationManager internal void AddServer(ServerStorage serverStorage) { - _configService.Current.ServerStorage[serverStorage.ServerUri] = serverStorage; + _configService.Current.ServerStorage.Add(serverStorage); Save(); } @@ -207,7 +203,7 @@ public class ServerConfigurationManager internal void DeleteServer(ServerStorage selectedServer) { - _configService.Current.ServerStorage.Remove(selectedServer.ServerUri); + _configService.Current.ServerStorage.Remove(selectedServer); Save(); } diff --git a/MareSynchronos/UI/DownloadUi.cs b/MareSynchronos/UI/DownloadUi.cs index be92553..e444eae 100644 --- a/MareSynchronos/UI/DownloadUi.cs +++ b/MareSynchronos/UI/DownloadUi.cs @@ -148,13 +148,11 @@ public class DownloadUi : WindowMediatorSubscriberBase var totalBytes = transfer.Value.Sum(c => c.Value.TotalBytes); var transferredBytes = transfer.Value.Sum(c => c.Value.TransferredBytes); - var downloadText = - $"{UiSharedService.ByteToString(transferredBytes, addSuffix: false)}/{UiSharedService.ByteToString(totalBytes)}"; var maxDlText = $"{UiSharedService.ByteToString(totalBytes, addSuffix: false)}/{UiSharedService.ByteToString(totalBytes)}"; - var textSize = ImGui.CalcTextSize(maxDlText); + var textSize = _configService.Current.TransferBarsShowText ? ImGui.CalcTextSize(maxDlText) : new Vector2(10, 10); - int dlBarHeight = (int)textSize.Y + 8; - int dlBarWidth = (int)textSize.X + 150; + int dlBarHeight = _configService.Current.TransferBarsHeight > ((int)textSize.Y + 5) ? _configService.Current.TransferBarsHeight : (int)textSize.Y + 5; + int dlBarWidth = _configService.Current.TransferBarsWidth > ((int)textSize.X + 10) ? _configService.Current.TransferBarsWidth : (int)textSize.X + 10; var dlBarStart = new Vector2(screenPos.X - dlBarWidth / 2f, screenPos.Y - dlBarHeight / 2f); var dlBarEnd = new Vector2(screenPos.X + dlBarWidth / 2f, screenPos.Y + dlBarHeight / 2f); @@ -172,10 +170,15 @@ public class DownloadUi : WindowMediatorSubscriberBase drawList.AddRectFilled(dlBarStart, dlBarEnd with { X = dlBarStart.X + (float)(dlProgressPercent * dlBarWidth) }, UiSharedService.Color(50, 205, 50, transparency), 1); - UiSharedService.DrawOutlinedFont(drawList, downloadText, - screenPos with { X = screenPos.X - textSize.X / 2f - 1, Y = screenPos.Y - textSize.Y / 2f - 1 }, - UiSharedService.Color(255, 255, 255, transparency), - UiSharedService.Color(0, 0, 0, transparency), 1); + + if (_configService.Current.TransferBarsShowText) + { + var downloadText = $"{UiSharedService.ByteToString(transferredBytes, addSuffix: false)}/{UiSharedService.ByteToString(totalBytes)}"; + UiSharedService.DrawOutlinedFont(drawList, downloadText, + screenPos with { X = screenPos.X - textSize.X / 2f - 1, Y = screenPos.Y - textSize.Y / 2f - 1 }, + UiSharedService.Color(255, 255, 255, transparency), + UiSharedService.Color(0, 0, 0, transparency), 1); + } } if (_configService.Current.ShowUploading) diff --git a/MareSynchronos/UI/SettingsUi.cs b/MareSynchronos/UI/SettingsUi.cs index 5ea7b46..1917d1e 100644 --- a/MareSynchronos/UI/SettingsUi.cs +++ b/MareSynchronos/UI/SettingsUi.cs @@ -172,6 +172,27 @@ public class SettingsUi : WindowMediatorSubscriberBase if (!showTransferBars) ImGui.BeginDisabled(); ImGui.Indent(); + bool transferBarShowText = _configService.Current.TransferBarsShowText; + if (ImGui.Checkbox("Show Download Text", ref transferBarShowText)) + { + _configService.Current.TransferBarsShowText = transferBarShowText; + _configService.Save(); + } + UiSharedService.DrawHelpText("Shows download text (amount of MiB downloaded) in the transfer bars"); + int transferBarWidth = _configService.Current.TransferBarsWidth; + if (ImGui.SliderInt("Transfer Bar Width", ref transferBarWidth, 10, 500)) + { + _configService.Current.TransferBarsWidth = transferBarWidth; + _configService.Save(); + } + UiSharedService.DrawHelpText("Width of the displayed transfer bars (will never be less wide than the displayed text)"); + int transferBarHeight = _configService.Current.TransferBarsHeight; + if (ImGui.SliderInt("Transfer Bar Height", ref transferBarHeight, 2, 50)) + { + _configService.Current.TransferBarsHeight = transferBarHeight; + _configService.Save(); + } + UiSharedService.DrawHelpText("Height of the displayed transfer bars (will never be less tall than the displayed text)"); bool showUploading = _configService.Current.ShowUploading; if (ImGui.Checkbox("Show 'Uploading' text below players that are currently uploading", ref showUploading)) { @@ -793,12 +814,20 @@ public class SettingsUi : WindowMediatorSubscriberBase if (ImGui.BeginTabItem("Service Settings")) { - var serverUri = selectedServer.ServerUri; - ImGui.InputText("Service URI", ref serverUri, 255, ImGuiInputTextFlags.ReadOnly); - UiSharedService.DrawHelpText("You cannot edit the service URI. Add a new service if you need to edit the URI."); var serverName = selectedServer.ServerName; + var serverUri = selectedServer.ServerUri; var isMain = string.Equals(serverName, ApiController.MainServer, StringComparison.OrdinalIgnoreCase); var flags = isMain ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None; + + if (ImGui.InputText("Service URI", ref serverUri, 255, flags)) + { + selectedServer.ServerUri = serverUri; + } + if (isMain) + { + UiSharedService.DrawHelpText("You cannot edit the URI of the main service."); + } + if (ImGui.InputText("Service Name", ref serverName, 255, flags)) { selectedServer.ServerName = serverName; @@ -808,6 +837,7 @@ public class SettingsUi : WindowMediatorSubscriberBase { UiSharedService.DrawHelpText("You cannot edit the name of the main service."); } + if (!isMain && selectedServer != _serverConfigurationManager.CurrentServer) { if (UiSharedService.IconTextButton(FontAwesomeIcon.Trash, "Delete Service") && UiSharedService.CtrlPressed()) diff --git a/MareSynchronos/UI/UISharedService.cs b/MareSynchronos/UI/UISharedService.cs index 7fd61eb..c50c58e 100644 --- a/MareSynchronos/UI/UISharedService.cs +++ b/MareSynchronos/UI/UISharedService.cs @@ -632,8 +632,10 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase string[] comboEntries = _serverConfigurationManager.GetServerNames(); if (_serverSelectionIndex == -1) + { _serverSelectionIndex = Array.IndexOf(_serverConfigurationManager.GetServerApiUrls(), _serverConfigurationManager.CurrentApiUrl); - if (_serverSelectionIndex == -1) + } + if (_serverSelectionIndex == -1 || _serverSelectionIndex >= comboEntries.Length) { _serverSelectionIndex = 0; } @@ -669,7 +671,7 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase { ImGui.SameLine(); var text = "Connect"; - if (_serverSelectionIndex == _serverConfigurationManager.GetCurrentServerIndex()) text = "Reconnect"; + if (_serverSelectionIndex == _serverConfigurationManager.CurrentServerIndex) text = "Reconnect"; if (IconTextButton(FontAwesomeIcon.Link, text)) { _serverConfigurationManager.SelectServer(_serverSelectionIndex);