Re-add performance thresholds and add whitelist/blacklist options

This commit is contained in:
Loporrit
2025-02-20 16:05:29 +00:00
parent 7918b54c92
commit 387aacdd6a
17 changed files with 704 additions and 64 deletions

View File

@@ -14,17 +14,19 @@ namespace MareSynchronos.Services;
public class GuiHookService : DisposableMediatorSubscriberBase
{
private readonly ILogger<GuiHookService> _logger;
private readonly DalamudUtilService _dalamudUtil;
private readonly MareConfigService _configService;
private readonly INamePlateGui _namePlateGui;
private readonly PairManager _pairManager;
private bool _isModified = false;
public GuiHookService(ILogger<GuiHookService> logger, MareMediator mediator, MareConfigService configService,
public GuiHookService(ILogger<GuiHookService> 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<PairHandlerVisibleMessage>(this, (_) => _namePlateGui.RequestRedraw());
Mediator.Subscribe<PairHandlerVisibleMessage>(this, (_) => RequestRedraw());
Mediator.Subscribe<NameplateRedrawMessage>(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<INamePlateUpdateHandler> 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)

View File

@@ -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

View File

@@ -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<PlayerPerformanceService> _logger;
private readonly MareMediator _mediator;
private readonly ServerConfigurationManager _serverConfigurationManager;
private readonly PlayerPerformanceConfigService _playerPerformanceConfigService;
private readonly Dictionary<string, bool> _warnedForPlayers = new(StringComparer.Ordinal);
public PlayerPerformanceService(ILogger<PlayerPerformanceService> 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<bool> 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<DownloadFileTransfer> 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;
}
}

View File

@@ -15,11 +15,16 @@ public class ServerConfigurationManager
private readonly ILogger<ServerConfigurationManager> _logger;
private readonly MareMediator _mareMediator;
private readonly NotesConfigService _notesConfig;
private readonly ServerBlockConfigService _blockConfig;
private readonly ServerTagConfigService _serverTagConfig;
private readonly SyncshellConfigService _syncshellConfig;
private HashSet<string>? CachedWhitelistedUIDs = null;
private HashSet<string>? CachedBlacklistedUIDs = null;
public ServerConfigurationManager(ILogger<ServerConfigurationManager> 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<string> Whitelist => CurrentBlockStorage().Whitelist;
public IReadOnlyList<string> 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();
}
}
}