diff --git a/MareSynchronos/FileCache/CacheMonitor.cs b/MareSynchronos/FileCache/CacheMonitor.cs index 7d9321d..f8cb4e3 100644 --- a/MareSynchronos/FileCache/CacheMonitor.cs +++ b/MareSynchronos/FileCache/CacheMonitor.cs @@ -96,8 +96,7 @@ public sealed class CacheMonitor : DisposableMediatorSubscriberBase public void HaltScan(string source) { - if (!HaltScanLocks.ContainsKey(source)) HaltScanLocks[source] = 0; - HaltScanLocks[source]++; + HaltScanLocks.AddOrUpdate(source, 1, (k, v) => v + 1); } record WatcherChange(WatcherChangeTypes ChangeType, string? OldPath = null); @@ -447,10 +446,7 @@ public sealed class CacheMonitor : DisposableMediatorSubscriberBase public void ResumeScan(string source) { - if (!HaltScanLocks.ContainsKey(source)) HaltScanLocks[source] = 0; - - HaltScanLocks[source]--; - if (HaltScanLocks[source] < 0) HaltScanLocks[source] = 0; + HaltScanLocks.AddOrUpdate(source, 0, (k, v) => Math.Max(0, v - 1)); } protected override void Dispose(bool disposing) diff --git a/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs b/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs index dd2e01e..9a22602 100644 --- a/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs +++ b/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs @@ -19,7 +19,8 @@ public class MareConfig : IMareConfiguration public DtrEntry.Colors DtrColorsNotConnected { get; set; } = new(Glow: 0x0428FFu); public DtrEntry.Colors DtrColorsPairsInRange { get; set; } = new(Glow: 0xFFBA47u); public bool UseNameColors { get; set; } = false; - public DtrEntry.Colors NameColors { get; set; } = new(Foreground: 0xF5EB67u, Glow: 0x78710Fu); + public DtrEntry.Colors NameColors { get; set; } = new(Foreground: 0x67EBF5u, Glow: 0x00303Cu); + public DtrEntry.Colors BlockedNameColors { get; set; } = new(Foreground: 0x8AADC7, Glow: 0x000080u); public bool EnableRightClickMenus { get; set; } = true; public NotificationLocation ErrorNotification { get; set; } = NotificationLocation.Both; public string ExportFolder { get; set; } = string.Empty; diff --git a/MareSynchronos/MareConfiguration/Configurations/PlayerPerformanceConfig.cs b/MareSynchronos/MareConfiguration/Configurations/PlayerPerformanceConfig.cs new file mode 100644 index 0000000..599910a --- /dev/null +++ b/MareSynchronos/MareConfiguration/Configurations/PlayerPerformanceConfig.cs @@ -0,0 +1,12 @@ +namespace MareSynchronos.MareConfiguration.Configurations; + +public class PlayerPerformanceConfig : IMareConfiguration +{ + public int Version { get; set; } = 1; + public bool AutoPausePlayersExceedingThresholds { get; set; } = false; + public bool NotifyAutoPauseDirectPairs { get; set; } = true; + public bool NotifyAutoPauseGroupPairs { get; set; } = false; + public int VRAMSizeAutoPauseThresholdMiB { get; set; } = 550; + public int TrisAutoPauseThresholdThousands { get; set; } = 250; + public bool IgnoreDirectPairs { get; set; } = true; +} \ No newline at end of file diff --git a/MareSynchronos/MareConfiguration/Configurations/ServerBlockConfig.cs b/MareSynchronos/MareConfiguration/Configurations/ServerBlockConfig.cs new file mode 100644 index 0000000..df2086a --- /dev/null +++ b/MareSynchronos/MareConfiguration/Configurations/ServerBlockConfig.cs @@ -0,0 +1,10 @@ +using MareSynchronos.MareConfiguration.Models; + +namespace MareSynchronos.MareConfiguration.Configurations; + +[Serializable] +public class ServerBlockConfig : IMareConfiguration +{ + public Dictionary ServerBlocks { get; set; } = new(StringComparer.Ordinal); + public int Version { get; set; } = 0; +} \ No newline at end of file diff --git a/MareSynchronos/MareConfiguration/Models/ServerBlockStorage.cs b/MareSynchronos/MareConfiguration/Models/ServerBlockStorage.cs new file mode 100644 index 0000000..642b9c2 --- /dev/null +++ b/MareSynchronos/MareConfiguration/Models/ServerBlockStorage.cs @@ -0,0 +1,8 @@ +namespace MareSynchronos.MareConfiguration.Models; + +[Serializable] +public class ServerBlockStorage +{ + public List Whitelist { get; set; } = new(); + public List Blacklist { get; set; } = new(); +} \ No newline at end of file diff --git a/MareSynchronos/MareConfiguration/PlayerPerformanceConfigService.cs b/MareSynchronos/MareConfiguration/PlayerPerformanceConfigService.cs new file mode 100644 index 0000000..e07eca1 --- /dev/null +++ b/MareSynchronos/MareConfiguration/PlayerPerformanceConfigService.cs @@ -0,0 +1,11 @@ +using MareSynchronos.MareConfiguration.Configurations; + +namespace MareSynchronos.MareConfiguration; + +public class PlayerPerformanceConfigService : ConfigurationServiceBase +{ + public const string ConfigName = "playerperformance.json"; + public PlayerPerformanceConfigService(string configDir) : base(configDir) { } + + protected override string ConfigurationName => ConfigName; +} \ No newline at end of file diff --git a/MareSynchronos/MareConfiguration/ServerBlockConfigService.cs b/MareSynchronos/MareConfiguration/ServerBlockConfigService.cs new file mode 100644 index 0000000..9aac787 --- /dev/null +++ b/MareSynchronos/MareConfiguration/ServerBlockConfigService.cs @@ -0,0 +1,14 @@ +using MareSynchronos.MareConfiguration.Configurations; + +namespace MareSynchronos.MareConfiguration; + +public class ServerBlockConfigService : ConfigurationServiceBase +{ + public const string ConfigName = "blocks.json"; + + public ServerBlockConfigService(string configDir) : base(configDir) + { + } + + protected override string ConfigurationName => ConfigName; +} \ No newline at end of file diff --git a/MareSynchronos/PlayerData/Factories/PairFactory.cs b/MareSynchronos/PlayerData/Factories/PairFactory.cs index b69f95d..ad56370 100644 --- a/MareSynchronos/PlayerData/Factories/PairFactory.cs +++ b/MareSynchronos/PlayerData/Factories/PairFactory.cs @@ -1,4 +1,5 @@ -using MareSynchronos.API.Dto.Group; +using MareSynchronos.API.Data; +using MareSynchronos.API.Dto.Group; using MareSynchronos.MareConfiguration; using MareSynchronos.PlayerData.Pairs; using MareSynchronos.Services.Mediator; @@ -25,8 +26,8 @@ public class PairFactory _serverConfigurationManager = serverConfigurationManager; } - public Pair Create() + public Pair Create(UserData userData) { - return new Pair(_loggerFactory.CreateLogger(), _cachedPlayerFactory, _mareMediator, _mareConfig, _serverConfigurationManager); + return new Pair(_loggerFactory.CreateLogger(), userData, _cachedPlayerFactory, _mareMediator, _mareConfig, _serverConfigurationManager); } } \ No newline at end of file diff --git a/MareSynchronos/PlayerData/Handlers/PairHandler.cs b/MareSynchronos/PlayerData/Handlers/PairHandler.cs index 1bff501..2b98d8f 100644 --- a/MareSynchronos/PlayerData/Handlers/PairHandler.cs +++ b/MareSynchronos/PlayerData/Handlers/PairHandler.cs @@ -102,6 +102,12 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase _downloadCancellationTokenSource = _downloadCancellationTokenSource?.CancelRecreate(); _applicationCancellationTokenSource = _applicationCancellationTokenSource?.CancelRecreate(); }); + Mediator.Subscribe(this, (msg) => + { + if (msg.UID != null && !msg.UID.Equals(Pair.UserData.UID, StringComparison.Ordinal)) return; + Logger.LogDebug("Recalculating performance for {uid}", Pair.UserData.UID); + pair.ApplyLastReceivedData(); + }); LastAppliedDataBytes = -1; } @@ -159,6 +165,21 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase SetUploading(isUploading: false); + if (Pair.IsDownloadBlocked) + { + var reasons = string.Join(", ", Pair.HoldDownloadReasons); + Mediator.Publish(new EventMessage(new Event(PlayerName, Pair.UserData, nameof(PairHandler), EventSeverity.Warning, + $"Not applying character data: {reasons}"))); + Logger.LogDebug("[BASE-{appBase}] Not applying due to hold: {reasons}", applicationBase, reasons); + var hasDiffMods = characterData.CheckUpdatedData(applicationBase, _cachedData, Logger, + this, forceApplyCustomization, forceApplyMods: false) + .Any(p => p.Value.Contains(PlayerChanges.ModManip) || p.Value.Contains(PlayerChanges.ModFiles)); + _forceApplyMods = hasDiffMods || _forceApplyMods || (PlayerCharacter == IntPtr.Zero && _cachedData == null); + _cachedData = characterData; + Logger.LogDebug("[BASE-{appBase}] Setting data: {hash}, forceApplyMods: {force}", applicationBase, _cachedData.DataHash.Value, _forceApplyMods); + return; + } + Logger.LogDebug("[BASE-{appbase}] Applying data for {player}, forceApplyCustomization: {forced}, forceApplyMods: {forceMods}", applicationBase, this, forceApplyCustomization, _forceApplyMods); Logger.LogDebug("[BASE-{appbase}] Hash for data is {newHash}, current cache hash is {oldHash}", applicationBase, characterData.DataHash.Value, _cachedData?.DataHash.Value ?? "NODATA"); @@ -220,6 +241,8 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase { base.Dispose(disposing); + if (!disposing) return; + SetUploading(isUploading: false); _downloadManager.Dispose(); var name = PlayerName; @@ -227,18 +250,52 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase try { Guid applicationId = Guid.NewGuid(); - _applicationCancellationTokenSource?.CancelDispose(); - _applicationCancellationTokenSource = null; - _downloadCancellationTokenSource?.CancelDispose(); - _downloadCancellationTokenSource = null; - _charaHandler?.Dispose(); - _charaHandler = null; if (!string.IsNullOrEmpty(name)) { Mediator.Publish(new EventMessage(new Event(name, Pair.UserData, nameof(PairHandler), EventSeverity.Informational, "Disposing User"))); } + if (!_lifetime.ApplicationStopping.IsCancellationRequested) + UndoApplicationAsync(applicationId).GetAwaiter().GetResult(); + + _applicationCancellationTokenSource?.Dispose(); + _applicationCancellationTokenSource = null; + _downloadCancellationTokenSource?.Dispose(); + _downloadCancellationTokenSource = null; + _charaHandler?.Dispose(); + _charaHandler = null; + } + catch (Exception ex) + { + Logger.LogWarning(ex, "Error on disposal of {name}", name); + } + finally + { + PlayerName = null; + _cachedData = null; + Logger.LogDebug("Disposing {name} complete", name); + } + } + + public void UndoApplication(Guid applicationId = default) + { + _ = Task.Run(async () => { + await UndoApplicationAsync(applicationId).ConfigureAwait(false); + }); + } + + private async Task UndoApplicationAsync(Guid applicationId = default) + { + Logger.LogDebug($"Undoing application of {Pair.UserPair}"); + var name = PlayerName; + try + { + if (applicationId == default) + applicationId = Guid.NewGuid(); + _applicationCancellationTokenSource = _applicationCancellationTokenSource?.CancelRecreate(); + _downloadCancellationTokenSource = _downloadCancellationTokenSource?.CancelRecreate(); + Logger.LogDebug("[{applicationId}] Removing Temp Collection for {name} ({user})", applicationId, name, Pair.UserPair); if (_penumbraCollection != Guid.Empty) { @@ -246,15 +303,13 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase _penumbraCollection = Guid.Empty; } - if (_lifetime.ApplicationStopping.IsCancellationRequested) return; - if (_dalamudUtil is { IsZoning: false, IsInCutscene: false } && !string.IsNullOrEmpty(name)) { Logger.LogTrace("[{applicationId}] Restoring state for {name} ({OnlineUser})", applicationId, name, Pair.UserPair); if (!IsVisible) { Logger.LogDebug("[{applicationId}] Restoring Glamourer for {name} ({user})", applicationId, name, Pair.UserPair); - _ipcManager.Glamourer.RevertByNameAsync(Logger, name, applicationId).GetAwaiter().GetResult(); + await _ipcManager.Glamourer.RevertByNameAsync(Logger, name, applicationId); } else { @@ -267,7 +322,7 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase { try { - RevertCustomizationDataAsync(item.Key, name, applicationId, cts.Token).GetAwaiter().GetResult(); + await RevertCustomizationDataAsync(item.Key, name, applicationId, cts.Token); } catch (InvalidOperationException ex) { @@ -282,13 +337,7 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase } catch (Exception ex) { - Logger.LogWarning(ex, "Error on disposal of {name}", name); - } - finally - { - PlayerName = null; - _cachedData = null; - Logger.LogDebug("Disposing {name} complete", name); + Logger.LogWarning(ex, "Error on undoing application of {name}", name); } } @@ -382,16 +431,20 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase _downloadCancellationTokenSource = _downloadCancellationTokenSource?.CancelRecreate() ?? new CancellationTokenSource(); var downloadToken = _downloadCancellationTokenSource.Token; - _ = DownloadAndApplyCharacterAsync(applicationBase, charaData, updatedData, updateModdedPaths, updateManip, downloadToken).ConfigureAwait(false); + _ = Task.Run(async () => { + await DownloadAndApplyCharacterAsync(applicationBase, charaData, updatedData, updateModdedPaths, updateManip, downloadToken).ConfigureAwait(false); + }); } private async Task DownloadAndApplyCharacterAsync(Guid applicationBase, CharacterData charaData, Dictionary> updatedData, bool updateModdedPaths, bool updateManip, CancellationToken downloadToken) { + Logger.LogTrace("[BASE-{appBase}] DownloadAndApplyCharacterAsync", applicationBase); Dictionary<(string GamePath, string? Hash), string> moddedPaths = []; if (updateModdedPaths) { + Logger.LogTrace("[BASE-{appBase}] DownloadAndApplyCharacterAsync > updateModdedPaths", applicationBase); int attempts = 0; List toDownloadReplacements = TryCalculateModdedDictionary(applicationBase, charaData, out moddedPaths, downloadToken); @@ -406,6 +459,7 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase if (!_playerPerformanceService.ComputeAndAutoPauseOnVRAMUsageThresholds(this, charaData, toDownloadFiles)) { + Pair.HoldApplication("IndividualPerformanceThreshold", maxValue: 1); _downloadManager.CancelDownload(); return; } @@ -427,11 +481,30 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase break; } - await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false); + await Task.Delay(TimeSpan.FromSeconds(2), downloadToken).ConfigureAwait(false); } - if (!await _playerPerformanceService.CheckBothThresholds(this, charaData).ConfigureAwait(false)) + bool exceedsThreshold = !await _playerPerformanceService.CheckBothThresholds(this, charaData).ConfigureAwait(false); + + if (exceedsThreshold) + Pair.HoldApplication("IndividualPerformanceThreshold", maxValue: 1); + else + Pair.UnholdApplication("IndividualPerformanceThreshold"); + + if (exceedsThreshold) + { + Logger.LogTrace("[BASE-{appBase}] Not applying due to performance thresholds", applicationBase); return; + } + } + + if (Pair.IsApplicationBlocked) + { + var reasons = string.Join(", ", Pair.HoldApplicationReasons); + Mediator.Publish(new EventMessage(new Event(PlayerName, Pair.UserData, nameof(PairHandler), EventSeverity.Warning, + $"Not applying character data: {reasons}"))); + Logger.LogTrace("[BASE-{appBase}] Not applying due to hold: {reasons}", applicationBase, reasons); + return; } downloadToken.ThrowIfCancellationRequested(); @@ -457,11 +530,20 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase private async Task ApplyCharacterDataAsync(Guid applicationBase, CharacterData charaData, Dictionary> updatedData, bool updateModdedPaths, bool updateManip, Dictionary<(string GamePath, string? Hash), string> moddedPaths, CancellationToken token) { + ushort objIndex = ushort.MaxValue; try { _applicationId = Guid.NewGuid(); Logger.LogDebug("[BASE-{applicationId}] Starting application task for {this}: {appId}", applicationBase, this, _applicationId); + if (_penumbraCollection == Guid.Empty) + { + if (objIndex == ushort.MaxValue) + objIndex = await _dalamudUtil.RunOnFrameworkThread(() => _charaHandler!.GetGameObject()!.ObjectIndex).ConfigureAwait(false); + _penumbraCollection = await _ipcManager.Penumbra.CreateTemporaryCollectionAsync(Logger, Pair.UserData.UID); + await _ipcManager.Penumbra.AssignTemporaryCollectionAsync(Logger, _penumbraCollection, objIndex); + } + Logger.LogDebug("[{applicationId}] Waiting for initial draw for for {handler}", _applicationId, _charaHandler); await _dalamudUtil.WaitWhileCharacterIsDrawing(Logger, _charaHandler!, _applicationId, 30000, token).ConfigureAwait(false); @@ -470,7 +552,8 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase if (updateModdedPaths) { // ensure collection is set - var objIndex = await _dalamudUtil.RunOnFrameworkThread(() => _charaHandler!.GetGameObject()!.ObjectIndex).ConfigureAwait(false); + if (objIndex == ushort.MaxValue) + objIndex = await _dalamudUtil.RunOnFrameworkThread(() => _charaHandler!.GetGameObject()!.ObjectIndex).ConfigureAwait(false); await _ipcManager.Penumbra.AssignTemporaryCollectionAsync(Logger, _penumbraCollection, objIndex).ConfigureAwait(false); await _ipcManager.Penumbra.SetTemporaryModsAsync(Logger, _applicationId, _penumbraCollection, @@ -577,10 +660,6 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase Logger.LogTrace("Reapplying Pet Names data for {this}", this); await _ipcManager.PetNames.SetPlayerData(PlayerCharacter, _cachedData.PetNamesData).ConfigureAwait(false); }); - - if (_penumbraCollection == Guid.Empty) - _penumbraCollection = _ipcManager.Penumbra.CreateTemporaryCollectionAsync(Logger, Pair.UserData.UID).GetAwaiter().GetResult(); - _ipcManager.Penumbra.AssignTemporaryCollectionAsync(Logger, _penumbraCollection, _charaHandler.GetGameObject()!.ObjectIndex).GetAwaiter().GetResult(); } private async Task RevertCustomizationDataAsync(ObjectKind objectKind, string name, Guid applicationId, CancellationToken cancelToken) diff --git a/MareSynchronos/PlayerData/Pairs/Pair.cs b/MareSynchronos/PlayerData/Pairs/Pair.cs index 8385de9..9f599e2 100644 --- a/MareSynchronos/PlayerData/Pairs/Pair.cs +++ b/MareSynchronos/PlayerData/Pairs/Pair.cs @@ -12,28 +12,33 @@ using MareSynchronos.Services.Mediator; using MareSynchronos.Services.ServerConfiguration; using MareSynchronos.Utils; using Microsoft.Extensions.Logging; +using System.Collections.Concurrent; namespace MareSynchronos.PlayerData.Pairs; -public class Pair +public class Pair : DisposableMediatorSubscriberBase { private readonly PairHandlerFactory _cachedPlayerFactory; private readonly SemaphoreSlim _creationSemaphore = new(1); private readonly ILogger _logger; - private readonly MareMediator _mediator; private readonly MareConfigService _mareConfig; private readonly ServerConfigurationManager _serverConfigurationManager; private CancellationTokenSource _applicationCts = new(); private OnlineUserIdentDto? _onlineUserIdentDto = null; - public Pair(ILogger logger, PairHandlerFactory cachedPlayerFactory, + public Pair(ILogger logger, UserData userData, PairHandlerFactory cachedPlayerFactory, MareMediator mediator, MareConfigService mareConfig, ServerConfigurationManager serverConfigurationManager) + : base(logger, mediator) { _logger = logger; _cachedPlayerFactory = cachedPlayerFactory; - _mediator = mediator; _mareConfig = mareConfig; _serverConfigurationManager = serverConfigurationManager; + + UserData = userData; + + Mediator.SubscribeKeyed(this, UserData.UID, (msg) => HoldApplication(msg.Source)); + Mediator.SubscribeKeyed(this, UserData.UID, (msg) => UnholdApplication(msg.Source)); } public Dictionary GroupPair { get; set; } = new(GroupDtoComparer.Instance); @@ -43,6 +48,16 @@ public class Pair public bool IsPaused => UserPair != null && UserPair.OtherPermissions.IsPaired() ? UserPair.OtherPermissions.IsPaused() || UserPair.OwnPermissions.IsPaused() : GroupPair.All(p => p.Key.GroupUserPermissions.IsPaused() || p.Value.GroupUserPermissions.IsPaused()); + // Download locks apply earlier in the process than Application locks + private ConcurrentDictionary HoldDownloadLocks { get; set; } = new(StringComparer.Ordinal); + private ConcurrentDictionary HoldApplicationLocks { get; set; } = new(StringComparer.Ordinal); + + public bool IsDownloadBlocked => HoldDownloadLocks.Any(f => f.Value > 0); + public bool IsApplicationBlocked => HoldApplicationLocks.Any(f => f.Value > 0) || IsDownloadBlocked; + + public IEnumerable HoldDownloadReasons => HoldDownloadLocks.Keys; + public IEnumerable HoldApplicationReasons => Enumerable.Concat(HoldDownloadLocks.Keys, HoldApplicationLocks.Keys); + public bool IsVisible => CachedPlayer?.IsVisible ?? false; public CharacterData? LastReceivedCharacterData { get; set; } public string? PlayerName => GetPlayerName(); @@ -52,7 +67,7 @@ public class Pair public long LastAppliedApproximateVRAMBytes { get; set; } = -1; public string Ident => _onlineUserIdentDto?.Ident ?? string.Empty; - public UserData UserData => UserPair?.User ?? GroupPair.First().Value.User; + public UserData UserData { get; init; } public UserPairDto? UserPair { get; set; } @@ -65,30 +80,58 @@ public class Pair args.AddMenuItem(new MenuItem() { Name = "Open Profile", - OnClicked = (a) => _mediator.Publish(new ProfileOpenStandaloneMessage(this)), + OnClicked = (a) => Mediator.Publish(new ProfileOpenStandaloneMessage(this)), PrefixColor = 559, PrefixChar = 'L' }); - args.AddMenuItem(new MenuItem() + if (!IsApplicationBlocked) { - Name = "Reapply last data", - OnClicked = (a) => ApplyLastReceivedData(forced: true), - PrefixColor = 559, - PrefixChar = 'L', - }); + args.AddMenuItem(new MenuItem() + { + Name = "Always Block Modded Appearance", + OnClicked = (a) => { + _serverConfigurationManager.AddBlacklistUid(UserData.UID); + HoldApplication("Blacklist", maxValue: 1); + }, + PrefixColor = 559, + PrefixChar = 'L', + }); + args.AddMenuItem(new MenuItem() + { + Name = "Reapply last data", + OnClicked = (a) => ApplyLastReceivedData(forced: true), + PrefixColor = 559, + PrefixChar = 'L', + }); + } + else + { + args.AddMenuItem(new MenuItem() + { + Name = "Always Allow Modded Appearance", + OnClicked = (a) => { + _serverConfigurationManager.AddWhitelistUid(UserData.UID); + UnholdApplication("Blacklist", skipApplication: true); + // FIXME: Manually applying here should not be needed here if everything else was written properly + ApplyLastReceivedData(forced: true); + }, + PrefixColor = 559, + PrefixChar = 'L', + }); + } if (UserPair != null) { args.AddMenuItem(new MenuItem() { Name = "Change Permissions", - OnClicked = (a) => _mediator.Publish(new OpenPermissionWindow(this)), + OnClicked = (a) => Mediator.Publish(new OpenPermissionWindow(this)), PrefixColor = 559, PrefixChar = 'L', }); args.AddMenuItem(new MenuItem() { Name = "Cycle pause state", - OnClicked = (a) => _mediator.Publish(new CyclePauseMessage(UserData)), + OnClicked = (a) => Mediator.Publish(new CyclePauseMessage(UserData)), PrefixColor = 559, PrefixChar = 'L', }); @@ -130,6 +173,10 @@ public class Pair { if (CachedPlayer == null) return; if (LastReceivedCharacterData == null) return; + if (IsDownloadBlocked) return; + + if (_serverConfigurationManager.IsUidBlacklisted(UserData.UID)) + HoldApplication("Blacklist", maxValue: 1); CachedPlayer.ApplyCharacterData(Guid.NewGuid(), RemoveNotSyncedFiles(LastReceivedCharacterData.DeepClone())!, forced); } @@ -230,6 +277,44 @@ public class Pair CachedPlayer?.SetUploading(); } + public void HoldApplication(string source, int maxValue = int.MaxValue) + { + _logger.LogDebug($"Holding {UserData.UID} for reason: {source}"); + bool wasHeld = IsApplicationBlocked; + HoldApplicationLocks.AddOrUpdate(source, 1, (k, v) => Math.Min(maxValue, v + 1)); + if (!wasHeld) + CachedPlayer?.UndoApplication(); + } + + public void UnholdApplication(string source, bool skipApplication = false) + { + _logger.LogDebug($"Un-holding {UserData.UID} for reason: {source}"); + bool wasHeld = IsApplicationBlocked; + HoldApplicationLocks.AddOrUpdate(source, 0, (k, v) => Math.Max(0, v - 1)); + HoldApplicationLocks.TryRemove(new(source, 0)); + if (!skipApplication && wasHeld && !IsApplicationBlocked) + ApplyLastReceivedData(forced: true); + } + + public void HoldDownloads(string source, int maxValue = int.MaxValue) + { + _logger.LogDebug($"Holding {UserData.UID} for reason: {source}"); + bool wasHeld = IsApplicationBlocked; + HoldDownloadLocks.AddOrUpdate(source, 1, (k, v) => Math.Min(maxValue, v + 1)); + if (!wasHeld) + CachedPlayer?.UndoApplication(); + } + + public void UnholdDownloads(string source, bool skipApplication = false) + { + _logger.LogDebug($"Un-holding {UserData.UID} for reason: {source}"); + bool wasHeld = IsApplicationBlocked; + HoldDownloadLocks.AddOrUpdate(source, 0, (k, v) => Math.Max(0, v - 1)); + HoldDownloadLocks.TryRemove(new(source, 0)); + if (!skipApplication && wasHeld && !IsApplicationBlocked) + ApplyLastReceivedData(forced: true); + } + private CharacterData? RemoveNotSyncedFiles(CharacterData? data) { _logger.LogTrace("Removing not synced files"); diff --git a/MareSynchronos/PlayerData/Pairs/PairManager.cs b/MareSynchronos/PlayerData/Pairs/PairManager.cs index 3ec2f59..38567ce 100644 --- a/MareSynchronos/PlayerData/Pairs/PairManager.cs +++ b/MareSynchronos/PlayerData/Pairs/PairManager.cs @@ -54,7 +54,7 @@ public sealed class PairManager : DisposableMediatorSubscriberBase public void AddGroupPair(GroupPairFullInfoDto dto) { if (!_allClientPairs.ContainsKey(dto.User)) - _allClientPairs[dto.User] = _pairFactory.Create(); + _allClientPairs[dto.User] = _pairFactory.Create(dto.User); var group = _allGroups[dto.Group]; _allClientPairs[dto.User].GroupPair[group] = dto; @@ -65,7 +65,7 @@ public sealed class PairManager : DisposableMediatorSubscriberBase { if (!_allClientPairs.ContainsKey(dto.User)) { - _allClientPairs[dto.User] = _pairFactory.Create(); + _allClientPairs[dto.User] = _pairFactory.Create(dto.User); } else { diff --git a/MareSynchronos/Plugin.cs b/MareSynchronos/Plugin.cs index 229478c..105edb0 100644 --- a/MareSynchronos/Plugin.cs +++ b/MareSynchronos/Plugin.cs @@ -124,6 +124,8 @@ public sealed class Plugin : IDalamudPlugin collection.AddSingleton((s) => new SyncshellConfigService(pluginInterface.ConfigDirectory.FullName)); collection.AddSingleton((s) => new TransientConfigService(pluginInterface.ConfigDirectory.FullName)); collection.AddSingleton((s) => new XivDataStorageService(pluginInterface.ConfigDirectory.FullName)); + collection.AddSingleton((s) => new PlayerPerformanceConfigService(pluginInterface.ConfigDirectory.FullName)); + collection.AddSingleton((s) => new ServerBlockConfigService(pluginInterface.ConfigDirectory.FullName)); collection.AddSingleton((s) => new ConfigurationMigrator(s.GetRequiredService>(), s.GetRequiredService())); collection.AddSingleton(); @@ -163,7 +165,7 @@ public sealed class Plugin : IDalamudPlugin s.GetRequiredService(), s.GetRequiredService(), s.GetRequiredService(), s.GetRequiredService>(), gameInteropProvider, chatGui, s.GetRequiredService(), s.GetRequiredService())); - collection.AddScoped((s) => new GuiHookService(s.GetRequiredService>(), s.GetRequiredService(), + collection.AddScoped((s) => new GuiHookService(s.GetRequiredService>(), s.GetRequiredService(), s.GetRequiredService(), s.GetRequiredService(), namePlateGui, s.GetRequiredService())); collection.AddHostedService(p => p.GetRequiredService()); @@ -178,7 +180,16 @@ public sealed class Plugin : IDalamudPlugin }) .Build(); - _ = _host.StartAsync(); + Task.Run(async () => { + try + { + await _host.StartAsync(); + } + catch (Exception e) + { + pluginLog.Error(e, "HostBuilder startup exception"); + } + }); } public void Dispose() diff --git a/MareSynchronos/Services/GuiHookService.cs b/MareSynchronos/Services/GuiHookService.cs index 5ec90cc..1478ab7 100644 --- a/MareSynchronos/Services/GuiHookService.cs +++ b/MareSynchronos/Services/GuiHookService.cs @@ -14,17 +14,19 @@ namespace MareSynchronos.Services; public class GuiHookService : DisposableMediatorSubscriberBase { private readonly ILogger _logger; + private readonly DalamudUtilService _dalamudUtil; private readonly MareConfigService _configService; private readonly INamePlateGui _namePlateGui; private readonly PairManager _pairManager; private bool _isModified = false; - public GuiHookService(ILogger logger, MareMediator mediator, MareConfigService configService, + public GuiHookService(ILogger logger, DalamudUtilService dalamudUtil, MareMediator mediator, MareConfigService configService, INamePlateGui namePlateGui, PairManager pairManager) : base(logger, mediator) { _logger = logger; + _dalamudUtil = dalamudUtil; _configService = configService; _namePlateGui = namePlateGui; _pairManager = pairManager; @@ -32,26 +34,32 @@ public class GuiHookService : DisposableMediatorSubscriberBase _namePlateGui.OnNamePlateUpdate += OnNamePlateUpdate; _namePlateGui.RequestRedraw(); - Mediator.Subscribe(this, (_) => _namePlateGui.RequestRedraw()); + Mediator.Subscribe(this, (_) => RequestRedraw()); + Mediator.Subscribe(this, (_) => RequestRedraw()); } - public void RequestRedraw() + public void RequestRedraw(bool force = false) { if (!_configService.Current.UseNameColors) { - if (!_isModified) + if (!_isModified && !force) return; _isModified = false; } - _namePlateGui.RequestRedraw(); + Task.Run(async () => { + await _dalamudUtil.RunOnFrameworkThread(() => _namePlateGui.RequestRedraw()); + }); } protected override void Dispose(bool disposing) { base.Dispose(disposing); _namePlateGui.OnNamePlateUpdate -= OnNamePlateUpdate; - _namePlateGui.RequestRedraw(); + + Task.Run(async () => { + await _dalamudUtil.RunOnFrameworkThread(() => _namePlateGui.RequestRedraw()); + }); } private void OnNamePlateUpdate(INamePlateUpdateContext context, IReadOnlyList handlers) @@ -59,13 +67,17 @@ public class GuiHookService : DisposableMediatorSubscriberBase if (!_configService.Current.UseNameColors) return; - var visibleUsersIds = _pairManager.GetOnlineUserPairs().Where(u => u.IsVisible && u.PlayerCharacterId != uint.MaxValue).Select(u => (ulong)u.PlayerCharacterId).ToHashSet(); - var colors = _configService.Current.NameColors; + var visibleUsers = _pairManager.GetOnlineUserPairs().Where(u => u.IsVisible && u.PlayerCharacterId != uint.MaxValue); + var visibleUsersIds = visibleUsers.Select(u => (ulong)u.PlayerCharacterId).ToHashSet(); + + var visibleUsersDict = visibleUsers.ToDictionary(u => (ulong)u.PlayerCharacterId); foreach (var handler in handlers) { if (visibleUsersIds.Contains(handler.GameObjectId)) { + var pair = visibleUsersDict[handler.GameObjectId]; + var colors = !pair.IsApplicationBlocked ? _configService.Current.NameColors : _configService.Current.BlockedNameColors; handler.NameParts.TextWrap = ( BuildColorStartSeString(colors), BuildColorEndSeString(colors) diff --git a/MareSynchronos/Services/Mediator/Messages.cs b/MareSynchronos/Services/Mediator/Messages.cs index f913eb1..91d758f 100644 --- a/MareSynchronos/Services/Mediator/Messages.cs +++ b/MareSynchronos/Services/Mediator/Messages.cs @@ -88,5 +88,11 @@ public record PenumbraDirectoryChangedMessage(string? ModDirectory) : MessageBas public record PenumbraRedrawCharacterMessage(ICharacter Character) : SameThreadMessage; public record UserChatMsgMessage(SignedChatMessage ChatMsg) : MessageBase; public record GroupChatMsgMessage(GroupDto GroupInfo, SignedChatMessage ChatMsg) : MessageBase; +public record RecalculatePerformanceMessage(string? UID) : MessageBase; +public record NameplateRedrawMessage : MessageBase; +public record HoldPairApplicationMessage(string UID, string Source) : KeyedMessage(UID); +public record UnholdPairApplicationMessage(string UID, string Source) : KeyedMessage(UID); +public record HoldPairDownloadsMessage(string UID, string Source) : KeyedMessage(UID); +public record UnholdPairDownloadsMessage(string UID, string Source) : KeyedMessage(UID); #pragma warning restore S2094 #pragma warning restore MA0048 // File name must match type name \ No newline at end of file diff --git a/MareSynchronos/Services/PlayerPerformanceService.cs b/MareSynchronos/Services/PlayerPerformanceService.cs index 3c0fd34..b454ee6 100644 --- a/MareSynchronos/Services/PlayerPerformanceService.cs +++ b/MareSynchronos/Services/PlayerPerformanceService.cs @@ -1,25 +1,40 @@ using MareSynchronos.API.Data; using MareSynchronos.FileCache; +using MareSynchronos.MareConfiguration; using MareSynchronos.PlayerData.Handlers; +using MareSynchronos.Services.Events; using MareSynchronos.Services.Mediator; +using MareSynchronos.Services.ServerConfiguration; +using MareSynchronos.UI; using MareSynchronos.WebAPI.Files.Models; using Microsoft.Extensions.Logging; namespace MareSynchronos.Services; -public class PlayerPerformanceService +public class PlayerPerformanceService : DisposableMediatorSubscriberBase { + // Limits that will still be enforced when no limits are enabled + public const int MaxVRAMUsageThreshold = 2000; // 2GB + public const int MaxTriUsageThreshold = 2000000; // 2 million triangles + private readonly FileCacheManager _fileCacheManager; private readonly XivDataAnalyzer _xivDataAnalyzer; private readonly ILogger _logger; private readonly MareMediator _mediator; + private readonly ServerConfigurationManager _serverConfigurationManager; + private readonly PlayerPerformanceConfigService _playerPerformanceConfigService; + private readonly Dictionary _warnedForPlayers = new(StringComparer.Ordinal); public PlayerPerformanceService(ILogger logger, MareMediator mediator, - FileCacheManager fileCacheManager, + ServerConfigurationManager serverConfigurationManager, + PlayerPerformanceConfigService playerPerformanceConfigService, FileCacheManager fileCacheManager, XivDataAnalyzer xivDataAnalyzer) + : base(logger, mediator) { _logger = logger; _mediator = mediator; + _serverConfigurationManager = serverConfigurationManager; + _playerPerformanceConfigService = playerPerformanceConfigService; _fileCacheManager = fileCacheManager; _xivDataAnalyzer = xivDataAnalyzer; } @@ -36,6 +51,7 @@ public class PlayerPerformanceService public async Task CheckTriangleUsageThresholds(PairHandler pairHandler, CharacterData charaData) { + var config = _playerPerformanceConfigService.Current; var pair = pairHandler.Pair; long triUsage = 0; @@ -58,13 +74,42 @@ public class PlayerPerformanceService pair.LastAppliedDataTris = triUsage; - _logger.LogDebug("Calculated VRAM usage for {p}", pairHandler); + _logger.LogDebug("Calculated Triangle usage for {p}", pairHandler); + + long triUsageThreshold = config.TrisAutoPauseThresholdThousands * 1000; + bool isDirect = pair.UserPair != null; + bool autoPause = config.AutoPausePlayersExceedingThresholds; + bool notify = isDirect ? config.NotifyAutoPauseDirectPairs : config.NotifyAutoPauseGroupPairs; + + if (autoPause && isDirect && config.IgnoreDirectPairs) + autoPause = false; + + if (!autoPause || _serverConfigurationManager.IsUidWhitelisted(pair.UserData.UID)) + triUsageThreshold = MaxTriUsageThreshold; + + if (triUsage > triUsageThreshold) + { + if (notify && !pair.IsApplicationBlocked) + { + _mediator.Publish(new NotificationMessage($"{pair.PlayerName} ({pair.UserData.AliasOrUID}) automatically blocked", + $"Player {pair.PlayerName} ({pair.UserData.AliasOrUID}) exceeded your configured triangle auto block threshold (" + + $"{triUsage}/{triUsageThreshold} triangles)" + + $" and has been automatically blocked.", + MareConfiguration.Models.NotificationType.Warning)); + } + + _mediator.Publish(new EventMessage(new Event(pair.PlayerName, pair.UserData, nameof(PlayerPerformanceService), EventSeverity.Warning, + $"Exceeds triangle threshold: ({triUsage}/{triUsageThreshold} triangles)"))); + + return false; + } return true; } public bool ComputeAndAutoPauseOnVRAMUsageThresholds(PairHandler pairHandler, CharacterData charaData, List toDownloadFiles) { + var config = _playerPerformanceConfigService.Current; var pair = pairHandler.Pair; long vramUsage = 0; @@ -110,6 +155,34 @@ public class PlayerPerformanceService _logger.LogDebug("Calculated VRAM usage for {p}", pairHandler); + long vramUsageThreshold = config.VRAMSizeAutoPauseThresholdMiB; + bool isDirect = pair.UserPair != null; + bool autoPause = config.AutoPausePlayersExceedingThresholds; + bool notify = isDirect ? config.NotifyAutoPauseDirectPairs : config.NotifyAutoPauseGroupPairs; + + if (autoPause && isDirect && config.IgnoreDirectPairs) + autoPause = false; + + if (!autoPause || _serverConfigurationManager.IsUidWhitelisted(pair.UserData.UID)) + vramUsageThreshold = MaxVRAMUsageThreshold; + + if (vramUsage > vramUsageThreshold * 1024 * 1024) + { + if (notify && !pair.IsApplicationBlocked) + { + _mediator.Publish(new NotificationMessage($"{pair.PlayerName} ({pair.UserData.AliasOrUID}) automatically blocked", + $"Player {pair.PlayerName} ({pair.UserData.AliasOrUID}) exceeded your configured VRAM auto block threshold (" + + $"{UiSharedService.ByteToString(vramUsage, addSuffix: true)}/{vramUsageThreshold}MiB)" + + $" and has been automatically blocked.", + MareConfiguration.Models.NotificationType.Warning)); + } + + _mediator.Publish(new EventMessage(new Event(pair.PlayerName, pair.UserData, nameof(PlayerPerformanceService), EventSeverity.Warning, + $"Exceeds VRAM threshold: ({UiSharedService.ByteToString(vramUsage, addSuffix: true)}/{vramUsageThreshold} MiB)"))); + + return false; + } + return true; } } \ No newline at end of file diff --git a/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs b/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs index fa11f50..1ca62d6 100644 --- a/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs +++ b/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs @@ -15,11 +15,16 @@ public class ServerConfigurationManager private readonly ILogger _logger; private readonly MareMediator _mareMediator; private readonly NotesConfigService _notesConfig; + private readonly ServerBlockConfigService _blockConfig; private readonly ServerTagConfigService _serverTagConfig; private readonly SyncshellConfigService _syncshellConfig; + private HashSet? CachedWhitelistedUIDs = null; + private HashSet? CachedBlacklistedUIDs = null; + public ServerConfigurationManager(ILogger logger, ServerConfigService configService, ServerTagConfigService serverTagConfig, SyncshellConfigService syncshellConfig, NotesConfigService notesConfig, + ServerBlockConfigService blockConfig, DalamudUtilService dalamudUtil, MareMediator mareMediator) { _logger = logger; @@ -27,6 +32,7 @@ public class ServerConfigurationManager _serverTagConfig = serverTagConfig; _syncshellConfig = syncshellConfig; _notesConfig = notesConfig; + _blockConfig = blockConfig; _dalamudUtil = dalamudUtil; _mareMediator = mareMediator; EnsureMainExists(); @@ -35,11 +41,16 @@ public class ServerConfigurationManager public string CurrentApiUrl => CurrentServer.ServerUri; public ServerStorage CurrentServer => _configService.Current.ServerStorage[CurrentServerIndex]; + public IReadOnlyList Whitelist => CurrentBlockStorage().Whitelist; + public IReadOnlyList Blacklist => CurrentBlockStorage().Blacklist; + public int CurrentServerIndex { set { _configService.Current.CurrentServer = value; + CachedWhitelistedUIDs = null; + CachedBlacklistedUIDs = null; _configService.Save(); } get @@ -403,6 +414,54 @@ public class ServerConfigurationManager _syncshellConfig.Save(); } + internal bool IsUidWhitelisted(string uid) + { + CachedWhitelistedUIDs ??= [.. CurrentBlockStorage().Whitelist]; + return CachedWhitelistedUIDs.Contains(uid); + } + + internal bool IsUidBlacklisted(string uid) + { + CachedBlacklistedUIDs ??= [.. CurrentBlockStorage().Blacklist]; + return CachedBlacklistedUIDs.Contains(uid); + } + + internal void AddWhitelistUid(string uid) + { + if (IsUidWhitelisted(uid)) + return; + if (CurrentBlockStorage().Blacklist.RemoveAll(u => u == uid) > 0) + CachedBlacklistedUIDs = null; + CurrentBlockStorage().Whitelist.Add(uid); + CachedWhitelistedUIDs = null; + _blockConfig.Save(); + } + + internal void AddBlacklistUid(string uid) + { + if (IsUidBlacklisted(uid)) + return; + if (CurrentBlockStorage().Whitelist.RemoveAll(u => u == uid) > 0) + CachedWhitelistedUIDs = null; + CurrentBlockStorage().Blacklist.Add(uid); + CachedBlacklistedUIDs = null; + _blockConfig.Save(); + } + + internal void RemoveWhitelistUid(string uid) + { + if (CurrentBlockStorage().Whitelist.RemoveAll(u => u == uid) > 0) + CachedWhitelistedUIDs = null; + _blockConfig.Save(); + } + + internal void RemoveBlacklistUid(string uid) + { + if (CurrentBlockStorage().Blacklist.RemoveAll(u => u == uid) > 0) + CachedBlacklistedUIDs = null; + _blockConfig.Save(); + } + private ServerNotesStorage CurrentNotesStorage() { TryCreateCurrentNotesStorage(); @@ -421,6 +480,12 @@ public class ServerConfigurationManager return _syncshellConfig.Current.ServerShellStorage[CurrentApiUrl]; } + private ServerBlockStorage CurrentBlockStorage() + { + TryCreateCurrentBlockStorage(); + return _blockConfig.Current.ServerBlocks[CurrentApiUrl]; + } + private void EnsureMainExists() { bool lopExists = false; @@ -478,4 +543,12 @@ public class ServerConfigurationManager _syncshellConfig.Current.ServerShellStorage[CurrentApiUrl] = new(); } } + + private void TryCreateCurrentBlockStorage() + { + if (!_blockConfig.Current.ServerBlocks.ContainsKey(CurrentApiUrl)) + { + _blockConfig.Current.ServerBlocks[CurrentApiUrl] = new(); + } + } } \ No newline at end of file diff --git a/MareSynchronos/UI/SettingsUi.cs b/MareSynchronos/UI/SettingsUi.cs index f4ba59c..80e59a6 100644 --- a/MareSynchronos/UI/SettingsUi.cs +++ b/MareSynchronos/UI/SettingsUi.cs @@ -52,6 +52,8 @@ public class SettingsUi : WindowMediatorSubscriberBase private readonly GuiHookService _guiHookService; private readonly PerformanceCollectorService _performanceCollector; private readonly ServerConfigurationManager _serverConfigurationManager; + private readonly PlayerPerformanceConfigService _playerPerformanceConfigService; + private readonly PlayerPerformanceService _playerPerformanceService; private readonly UiSharedService _uiShared; private bool _deleteAccountPopupModalShown = false; private bool _deleteFilesPopupModalShown = false; @@ -76,6 +78,7 @@ public class SettingsUi : WindowMediatorSubscriberBase UiSharedService uiShared, MareConfigService configService, MareCharaFileManager mareCharaFileManager, PairManager pairManager, ChatService chatService, GuiHookService guiHookService, ServerConfigurationManager serverConfigurationManager, + PlayerPerformanceConfigService playerPerformanceConfigService, PlayerPerformanceService playerPerformanceService, MareMediator mediator, PerformanceCollectorService performanceCollector, FileUploadManager fileTransferManager, FileTransferOrchestrator fileTransferOrchestrator, @@ -90,6 +93,8 @@ public class SettingsUi : WindowMediatorSubscriberBase _chatService = chatService; _guiHookService = guiHookService; _serverConfigurationManager = serverConfigurationManager; + _playerPerformanceConfigService = playerPerformanceConfigService; + _playerPerformanceService = playerPerformanceService; _performanceCollector = performanceCollector; _fileTransferManager = fileTransferManager; _fileTransferOrchestrator = fileTransferOrchestrator; @@ -673,6 +678,18 @@ public class SettingsUi : WindowMediatorSubscriberBase _configService.Current.LogEvents = logEvents; _configService.Save(); } + + ImGui.Separator(); + _uiShared.BigText("Active Character Blocks"); + foreach (var pair in _pairManager.GetOnlineUserPairs()) + { + if (pair.IsApplicationBlocked) + { + ImGui.TextUnformatted(pair.PlayerName); + ImGui.SameLine(); + ImGui.TextUnformatted(string.Join(", ", pair.HoldApplicationReasons)); + } + } } private void DrawFileStorageSettings() @@ -1055,6 +1072,7 @@ public class SettingsUi : WindowMediatorSubscriberBase var useNameColors = _configService.Current.UseNameColors; var nameColors = _configService.Current.NameColors; + var autoPausedNameColors = _configService.Current.BlockedNameColors; if (ImGui.Checkbox("Color nameplates of paired players", ref useNameColors)) { _configService.Current.UseNameColors = useNameColors; @@ -1071,6 +1089,15 @@ public class SettingsUi : WindowMediatorSubscriberBase _configService.Save(); _guiHookService.RequestRedraw(); } + + ImGui.SameLine(); + + if (InputDtrColors("Blocked Character Color", ref autoPausedNameColors)) + { + _configService.Current.BlockedNameColors = autoPausedNameColors; + _configService.Save(); + _guiHookService.RequestRedraw(); + } } if (ImGui.Checkbox("Show separate Visible group", ref showVisibleSeparate)) @@ -1205,6 +1232,215 @@ public class SettingsUi : WindowMediatorSubscriberBase } } + private bool _perfUnapplied = false; + + private void DrawPerformance() + { + _uiShared.BigText("Performance Settings"); + UiSharedService.TextWrapped("The configuration options here are to give you more informed warnings and automation when it comes to other performance-intensive synced players."); + ImGui.Separator(); + bool recalculatePerformance = false; + string? recalculatePerformanceUID = null; + + _uiShared.BigText("Individual Limits"); + bool autoPause = _playerPerformanceConfigService.Current.AutoPausePlayersExceedingThresholds; + if (ImGui.Checkbox("Automatically block players exceeding thresholds", ref autoPause)) + { + _playerPerformanceConfigService.Current.AutoPausePlayersExceedingThresholds = autoPause; + _playerPerformanceConfigService.Save(); + recalculatePerformance = true; + } + _uiShared.DrawHelpText("When enabled, it will automatically block the modded appearance of all players that exceed the thresholds defined below." + Environment.NewLine + + "Will print a warning in chat when a player is blocked automatically."); + using (ImRaii.Disabled(!autoPause)) + { + using var indent = ImRaii.PushIndent(); + var notifyDirectPairs = _playerPerformanceConfigService.Current.NotifyAutoPauseDirectPairs; + var notifyGroupPairs = _playerPerformanceConfigService.Current.NotifyAutoPauseGroupPairs; + if (ImGui.Checkbox("Display auto-block warnings for individual pairs", ref notifyDirectPairs)) + { + _playerPerformanceConfigService.Current.NotifyAutoPauseDirectPairs = notifyDirectPairs; + _playerPerformanceConfigService.Save(); + } + if (ImGui.Checkbox("Display auto-block warnings for syncshell pairs", ref notifyGroupPairs)) + { + _playerPerformanceConfigService.Current.NotifyAutoPauseGroupPairs = notifyGroupPairs; + _playerPerformanceConfigService.Save(); + } + var vramAuto = _playerPerformanceConfigService.Current.VRAMSizeAutoPauseThresholdMiB; + var trisAuto = _playerPerformanceConfigService.Current.TrisAutoPauseThresholdThousands; + ImGui.SetNextItemWidth(100 * ImGuiHelpers.GlobalScale); + if (ImGui.InputInt("Auto Block VRAM threshold", ref vramAuto)) + { + _playerPerformanceConfigService.Current.VRAMSizeAutoPauseThresholdMiB = vramAuto; + _playerPerformanceConfigService.Save(); + _perfUnapplied = true; + } + ImGui.SameLine(); + ImGui.Text("(MiB)"); + _uiShared.DrawHelpText("When a loading in player and their VRAM usage exceeds this amount, automatically blocks the synced player." + UiSharedService.TooltipSeparator + + "Default: 550 MiB"); + ImGui.SetNextItemWidth(100 * ImGuiHelpers.GlobalScale); + if (ImGui.InputInt("Auto Block Triangle threshold", ref trisAuto)) + { + _playerPerformanceConfigService.Current.TrisAutoPauseThresholdThousands = trisAuto; + _playerPerformanceConfigService.Save(); + _perfUnapplied = true; + } + ImGui.SameLine(); + ImGui.Text("(thousand triangles)"); + _uiShared.DrawHelpText("When a loading in player and their triangle count exceeds this amount, automatically blocks the synced player." + UiSharedService.TooltipSeparator + + "Default: 250 thousand"); + using (ImRaii.Disabled(!_perfUnapplied)) + { + if (ImGui.Button("Apply Changes Now")) + { + recalculatePerformance = true; + _perfUnapplied = false; + } + } + } + +#region Whitelist + ImGui.Separator(); + _uiShared.BigText("Whitelisted UIDs"); + bool ignoreDirectPairs = _playerPerformanceConfigService.Current.IgnoreDirectPairs; + if (ImGui.Checkbox("Whitelist all individual pairs", ref ignoreDirectPairs)) + { + _playerPerformanceConfigService.Current.IgnoreDirectPairs = ignoreDirectPairs; + _playerPerformanceConfigService.Save(); + recalculatePerformance = true; + } + _uiShared.DrawHelpText("Individual pairs will never be affected by auto blocks."); + ImGui.Dummy(new Vector2(5)); + UiSharedService.TextWrapped("The entries in the list below will be not have auto block thresholds enforced."); + ImGui.SetNextItemWidth(200 * ImGuiHelpers.GlobalScale); + var whitelistPos = ImGui.GetCursorPos(); + ImGui.SetCursorPosX(240 * ImGuiHelpers.GlobalScale); + ImGui.InputText("##whitelistuid", ref _uidToAddForIgnore, 20); + using (ImRaii.Disabled(string.IsNullOrEmpty(_uidToAddForIgnore))) + { + ImGui.SetCursorPosX(240 * ImGuiHelpers.GlobalScale); + if (_uiShared.IconTextButton(FontAwesomeIcon.Plus, "Add UID to whitelist")) + { + if (!_serverConfigurationManager.IsUidWhitelisted(_uidToAddForIgnore)) + { + _serverConfigurationManager.AddWhitelistUid(_uidToAddForIgnore); + recalculatePerformance = true; + recalculatePerformanceUID = _uidToAddForIgnore; + } + _uidToAddForIgnore = string.Empty; + } + } + ImGui.SetCursorPosX(240 * ImGuiHelpers.GlobalScale); + _uiShared.DrawHelpText("Hint: UIDs are case sensitive.\nVanity IDs are also acceptable."); + ImGui.Dummy(new Vector2(10)); + var playerList = _serverConfigurationManager.Whitelist; + if (_selectedEntry > playerList.Count - 1) + _selectedEntry = -1; + ImGui.SetNextItemWidth(200 * ImGuiHelpers.GlobalScale); + ImGui.SetCursorPosY(whitelistPos.Y); + using (var lb = ImRaii.ListBox("##whitelist")) + { + if (lb) + { + for (int i = 0; i < playerList.Count; i++) + { + bool shouldBeSelected = _selectedEntry == i; + if (ImGui.Selectable(playerList[i] + "##" + i, shouldBeSelected)) + { + _selectedEntry = i; + } + string? lastSeenName = _serverConfigurationManager.GetNameForUid(playerList[i]); + if (lastSeenName != null) + { + ImGui.SameLine(); + _uiShared.IconText(FontAwesomeIcon.InfoCircle); + UiSharedService.AttachToolTip($"Last seen name: {lastSeenName}"); + } + } + } + } + using (ImRaii.Disabled(_selectedEntry == -1)) + { + using var pushId = ImRaii.PushId("deleteSelectedWhitelist"); + if (_uiShared.IconTextButton(FontAwesomeIcon.Trash, "Delete selected UID")) + { + _serverConfigurationManager.RemoveWhitelistUid(_serverConfigurationManager.Whitelist[_selectedEntry]); + _selectedEntry = -1; + _playerPerformanceConfigService.Save(); + recalculatePerformance = true; + } + } +#endregion Whitelist + +#region Blacklist + ImGui.Separator(); + _uiShared.BigText("Blacklisted UIDs"); + UiSharedService.TextWrapped("The entries in the list below will never have their characters displayed."); + ImGui.SetNextItemWidth(200 * ImGuiHelpers.GlobalScale); + var blacklistPos = ImGui.GetCursorPos(); + ImGui.SetCursorPosX(240 * ImGuiHelpers.GlobalScale); + ImGui.InputText("##uid", ref _uidToAddForIgnoreBlacklist, 20); + using (ImRaii.Disabled(string.IsNullOrEmpty(_uidToAddForIgnoreBlacklist))) + { + ImGui.SetCursorPosX(240 * ImGuiHelpers.GlobalScale); + if (_uiShared.IconTextButton(FontAwesomeIcon.Plus, "Add UID to blacklist")) + { + if (!_serverConfigurationManager.IsUidBlacklisted(_uidToAddForIgnoreBlacklist)) + { + _serverConfigurationManager.AddBlacklistUid(_uidToAddForIgnoreBlacklist); + recalculatePerformance = true; + recalculatePerformanceUID = _uidToAddForIgnoreBlacklist; + } + _uidToAddForIgnoreBlacklist = string.Empty; + } + } + _uiShared.DrawHelpText("Hint: UIDs are case sensitive.\nVanity IDs are also acceptable."); + ImGui.Dummy(new Vector2(10)); + var blacklist = _serverConfigurationManager.Blacklist; + if (_selectedEntryBlacklist > blacklist.Count - 1) + _selectedEntryBlacklist = -1; + ImGui.SetNextItemWidth(200 * ImGuiHelpers.GlobalScale); + ImGui.SetCursorPosY(blacklistPos.Y); + using (var lb = ImRaii.ListBox("##blacklist")) + { + if (lb) + { + for (int i = 0; i < blacklist.Count; i++) + { + bool shouldBeSelected = _selectedEntryBlacklist == i; + if (ImGui.Selectable(blacklist[i] + "##BL" + i, shouldBeSelected)) + { + _selectedEntryBlacklist = i; + } + string? lastSeenName = _serverConfigurationManager.GetNameForUid(blacklist[i]); + if (lastSeenName != null) + { + ImGui.SameLine(); + _uiShared.IconText(FontAwesomeIcon.InfoCircle); + UiSharedService.AttachToolTip($"Last seen name: {lastSeenName}"); + } + } + } + } + using (ImRaii.Disabled(_selectedEntryBlacklist == -1)) + { + using var pushId = ImRaii.PushId("deleteSelectedBlacklist"); + if (_uiShared.IconTextButton(FontAwesomeIcon.Trash, "Delete selected UID")) + { + _serverConfigurationManager.RemoveBlacklistUid(_serverConfigurationManager.Blacklist[_selectedEntryBlacklist]); + _selectedEntryBlacklist = -1; + _playerPerformanceConfigService.Save(); + recalculatePerformance = true; + } + } +#endregion Blacklist + + if (recalculatePerformance) + Mediator.Publish(new RecalculatePerformanceMessage(recalculatePerformanceUID)); + } + private static bool InputDtrColors(string label, ref DtrEntry.Colors colors) { using var id = ImRaii.PushId(label); @@ -1580,6 +1816,12 @@ public class SettingsUi : WindowMediatorSubscriberBase } } + private string _uidToAddForIgnore = string.Empty; + private int _selectedEntry = -1; + + private string _uidToAddForIgnoreBlacklist = string.Empty; + private int _selectedEntryBlacklist = -1; + private void DrawSettingsContent() { if (_apiController.ServerState is ServerState.Connected) @@ -1605,6 +1847,12 @@ public class SettingsUi : WindowMediatorSubscriberBase ImGui.EndTabItem(); } + if (ImGui.BeginTabItem("Performance")) + { + DrawPerformance(); + ImGui.EndTabItem(); + } + if (ImGui.BeginTabItem("Export & Storage")) { DrawFileStorageSettings();