various 'improvements'

This commit is contained in:
2025-12-11 12:59:32 +09:00
parent 2e14fc2f8f
commit 6cf0e3daed
26 changed files with 3706 additions and 884 deletions

View File

@@ -1,4 +1,3 @@
using LightlessSync.API.Dto;
using LightlessSync.API.Dto.Chat;
using LightlessSync.Services.ActorTracking;
using LightlessSync.Services.Mediator;
@@ -21,12 +20,11 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
private const int MaxReportContextLength = 1000;
private readonly ApiController _apiController;
private readonly ChatConfigService _chatConfigService;
private readonly DalamudUtilService _dalamudUtilService;
private readonly ActorObjectService _actorObjectService;
private readonly PairUiService _pairUiService;
private readonly object _sync = new();
private readonly Lock _sync = new();
private readonly Dictionary<string, ChatChannelState> _channels = new(StringComparer.Ordinal);
private readonly List<string> _channelOrder = new();
@@ -55,7 +53,6 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
PairUiService pairUiService)
: base(logger, mediator)
{
_chatConfigService = chatConfigService;
_apiController = apiController;
_dalamudUtilService = dalamudUtilService;
_actorObjectService = actorObjectService;
@@ -63,12 +60,12 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
_isLoggedIn = _dalamudUtilService.IsLoggedIn;
_isConnected = _apiController.IsConnected;
_chatEnabled = _chatConfigService.Current.AutoEnableChatOnLogin;
_chatEnabled = chatConfigService.Current.AutoEnableChatOnLogin;
}
public IReadOnlyList<ChatChannelSnapshot> GetChannelsSnapshot()
{
lock (_sync)
using (_sync.EnterScope())
{
var snapshots = new List<ChatChannelSnapshot>(_channelOrder.Count);
foreach (var key in _channelOrder)
@@ -107,7 +104,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
{
get
{
lock (_sync)
using (_sync.EnterScope())
{
return _chatEnabled;
}
@@ -118,7 +115,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
{
get
{
lock (_sync)
using (_sync.EnterScope())
{
return _chatEnabled && _isConnected;
}
@@ -127,7 +124,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
public void SetActiveChannel(string? key)
{
lock (_sync)
using (_sync.EnterScope())
{
_activeChannelKey = key;
if (key is not null && _channels.TryGetValue(key, out var state))
@@ -145,7 +142,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
private async Task EnableChatAsync()
{
bool wasEnabled;
lock (_sync)
using (_sync.EnterScope())
{
wasEnabled = _chatEnabled;
if (!wasEnabled)
@@ -170,7 +167,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
List<ChatChannelDescriptor> groupDescriptors;
ChatChannelDescriptor? zoneDescriptor;
lock (_sync)
using (_sync.EnterScope())
{
wasEnabled = _chatEnabled;
if (!wasEnabled)
@@ -259,7 +256,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
return Task.FromResult(new ChatReportResult(false, "Please describe why you are reporting this message."));
}
lock (_sync)
using (_sync.EnterScope())
{
if (!_chatEnabled)
{
@@ -311,8 +308,8 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
Mediator.Subscribe<DalamudLogoutMessage>(this, _ => HandleLogout());
Mediator.Subscribe<ZoneSwitchEndMessage>(this, _ => ScheduleZonePresenceUpdate());
Mediator.Subscribe<WorldChangedMessage>(this, _ => ScheduleZonePresenceUpdate(force: true));
Mediator.Subscribe<ConnectedMessage>(this, msg => HandleConnected(msg.Connection));
Mediator.Subscribe<HubReconnectedMessage>(this, _ => HandleConnected(null));
Mediator.Subscribe<ConnectedMessage>(this, _ => HandleConnected());
Mediator.Subscribe<HubReconnectedMessage>(this, _ => HandleConnected());
Mediator.Subscribe<HubReconnectingMessage>(this, _ => HandleReconnecting());
Mediator.Subscribe<DisconnectedMessage>(this, _ => HandleReconnecting());
Mediator.Subscribe<PairUiUpdatedMessage>(this, _ => RefreshGroupsFromPairManager());
@@ -371,11 +368,11 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
}
}
private void HandleConnected(ConnectionDto? connection)
private void HandleConnected()
{
_isConnected = true;
lock (_sync)
using (_sync.EnterScope())
{
_selfTokens.Clear();
_pendingSelfMessages.Clear();
@@ -410,7 +407,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
{
_isConnected = false;
lock (_sync)
using (_sync.EnterScope())
{
_selfTokens.Clear();
_pendingSelfMessages.Clear();
@@ -475,7 +472,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
private void UpdateChannelsForDisabledState()
{
lock (_sync)
using (_sync.EnterScope())
{
foreach (var state in _channels.Values)
{
@@ -513,7 +510,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
string? zoneKey;
ZoneChannelDefinition? definition = null;
lock (_sync)
using (_sync.EnterScope())
{
_territoryToZoneKey.TryGetValue(territoryId, out zoneKey);
if (zoneKey is not null)
@@ -538,7 +535,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
bool shouldForceSend;
lock (_sync)
using (_sync.EnterScope())
{
var state = EnsureZoneStateLocked();
state.DisplayName = definition.Value.DisplayName;
@@ -566,7 +563,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
ChatChannelDescriptor? descriptor = null;
bool clearedHistory = false;
lock (_sync)
using (_sync.EnterScope())
{
descriptor = _lastZoneDescriptor;
_lastZoneDescriptor = null;
@@ -590,9 +587,9 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
state.DisplayName = "Zone Chat";
}
if (_activeChannelKey == ZoneChannelKey)
if (string.Equals(_activeChannelKey, ZoneChannelKey, StringComparison.Ordinal))
{
_activeChannelKey = _channelOrder.FirstOrDefault(key => key != ZoneChannelKey);
_activeChannelKey = _channelOrder.FirstOrDefault(key => !string.Equals(key, ZoneChannelKey, StringComparison.Ordinal));
}
}
@@ -627,7 +624,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
{
var infoList = infos ?? Array.Empty<ZoneChatChannelInfoDto>();
lock (_sync)
using (_sync.EnterScope())
{
_zoneDefinitions.Clear();
_territoryToZoneKey.Clear();
@@ -657,7 +654,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
{
if (def.TerritoryNames.Contains(variant))
{
_territoryToZoneKey[(uint)kvp.Key] = def.Key;
_territoryToZoneKey[kvp.Key] = def.Key;
break;
}
}
@@ -689,7 +686,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
var descriptorsToJoin = new List<ChatChannelDescriptor>();
var descriptorsToLeave = new List<ChatChannelDescriptor>();
lock (_sync)
using (_sync.EnterScope())
{
var remainingGroups = new HashSet<string>(_groupDefinitions.Keys, StringComparer.OrdinalIgnoreCase);
@@ -807,7 +804,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
return;
List<ChatChannelDescriptor> descriptors;
lock (_sync)
using (_sync.EnterScope())
{
descriptors = _channels.Values
.Where(state => state.Type == ChatChannelType.Group)
@@ -832,7 +829,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
var presenceKey = BuildPresenceKey(descriptor);
bool stateMatches;
lock (_sync)
using (_sync.EnterScope())
{
stateMatches = !force
&& _lastPresenceStates.TryGetValue(presenceKey, out var lastState)
@@ -846,7 +843,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
{
await _apiController.UpdateChatPresence(new ChatPresenceUpdateDto(descriptor, territoryId, isActive)).ConfigureAwait(false);
lock (_sync)
using (_sync.EnterScope())
{
if (isActive)
{
@@ -870,7 +867,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
var key = normalized.Type == ChatChannelType.Zone ? ZoneChannelKey : BuildChannelKey(normalized);
var pending = new PendingSelfMessage(key, message);
lock (_sync)
using (_sync.EnterScope())
{
_pendingSelfMessages.Add(pending);
while (_pendingSelfMessages.Count > 20)
@@ -884,7 +881,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
private void RemovePendingSelfMessage(PendingSelfMessage pending)
{
lock (_sync)
using (_sync.EnterScope())
{
var index = _pendingSelfMessages.FindIndex(p =>
string.Equals(p.ChannelKey, pending.ChannelKey, StringComparison.Ordinal) &&
@@ -905,7 +902,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
var message = BuildMessage(dto, fromSelf);
bool publishChannelList = false;
lock (_sync)
using (_sync.EnterScope())
{
if (!_channels.TryGetValue(key, out var state))
{
@@ -960,7 +957,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
if (publishChannelList)
{
lock (_sync)
using (_sync.EnterScope())
{
UpdateChannelOrderLocked();
}
@@ -973,7 +970,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
{
if (dto.Sender.User?.UID is { } uid && string.Equals(uid, _apiController.UID, StringComparison.Ordinal))
{
lock (_sync)
using (_sync.EnterScope())
{
_selfTokens[channelKey] = dto.Sender.Token;
}
@@ -981,7 +978,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
return true;
}
lock (_sync)
using (_sync.EnterScope())
{
if (_selfTokens.TryGetValue(channelKey, out var token) &&
string.Equals(token, dto.Sender.Token, StringComparison.Ordinal))
@@ -1014,7 +1011,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
{
var isZone = dto.Channel.Type == ChatChannelType.Zone;
if (!string.IsNullOrEmpty(dto.Sender.HashedCid) &&
_actorObjectService.TryGetActorByHash(dto.Sender.HashedCid, out var descriptor) &&
_actorObjectService.TryGetValidatedActorByHash(dto.Sender.HashedCid, out var descriptor) &&
!string.IsNullOrWhiteSpace(descriptor.Name))
{
return descriptor.Name;
@@ -1065,7 +1062,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
{
_activeChannelKey = _channelOrder[0];
}
else if (_activeChannelKey is not null && !_channelOrder.Contains(_activeChannelKey))
else if (_activeChannelKey is not null && !_channelOrder.Contains(_activeChannelKey, StringComparer.Ordinal))
{
_activeChannelKey = _channelOrder.Count > 0 ? _channelOrder[0] : null;
}
@@ -1108,7 +1105,7 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
private static bool ChannelDescriptorsMatch(ChatChannelDescriptor left, ChatChannelDescriptor right)
=> left.Type == right.Type
&& NormalizeKey(left.CustomKey) == NormalizeKey(right.CustomKey)
&& string.Equals(NormalizeKey(left.CustomKey), NormalizeKey(right.CustomKey), StringComparison.Ordinal)
&& left.WorldId == right.WorldId;
private ChatChannelState EnsureZoneStateLocked()
@@ -1180,6 +1177,3 @@ public sealed class ZoneChatService : DisposableMediatorSubscriberBase, IHostedS
private readonly record struct PendingSelfMessage(string ChannelKey, string Message);
}