Some checks failed
Tag and Release Lightless / tag-and-release (push) Failing after 32s
 Lightless 1.12.0 is HERE! In this major update, we are introducing something we've been working on and testing for the last couple of weeks. In this update we are introducing a new (**OPTIONAL**) feature called **LightFinder**! We took inspiration from FFXIV's very own Party Finder and decided to implement something that allows users to not only look for fellow Lightless users, but also put up their Syncshell for others looking to join a sync community! When you enable LightFinder, you will be visible to other LightFinder users for **3 hours** or when you want to disabled it. When the 3 hours are up, you can either leave it disabled or enable it again for another 3 hours. The tag shown above will show above your nameplate, and you will be able to receive pair requests in your UI from other users with LightFinder enabled without having to input their uid!  Are you at a Venue? In Limsa? Partying in the streets of Uldah? If you're looking for fellow Lightless users you can now enable LightFinder and you will be shown to others who also have LightFinder enabled! Looking for a Syncshell to join? Enable LightFinder to see what SyncShells are available to join! Want to advertise your Syncshell? Choose the syncshell you want to put up in LightFinder and enable LightFinder. **IMPORTANT: We want to stress the fact that, if you just want to sync with just your friends and no one else, this does not take that away. No one will know you use Lightless unless you choose to tell them, or use this **OPTIONAL** feature. Your privacy is still maintained if you don't want to use the feature.** # Major Changes ## LightFinder - **OPTIONAL FEATURE** - **DOES NOT AFFECT YOU IF YOU DON'T WANT TO USE IT**  * New **OPTIONAL** syncing feature where one can enable something like a Party Finder so that other Lightless users in the area can see you are looking for people to sync with. Enable it by clicking the compass button and then the `Enable LightFinder` button.  * [L] Send Pair Request has been added to player context menus. You should still be able to send a request without Lightfinder on BUT you will need to know the other player is using Lightless and have them send a pair request back.  * When in LightFinder mode, for X mins you will be visible to all Lightless Users WHO ALSO HAVE LIGHTFINDER ON and will receive notifications of people wanting to pair with you. If you are the person using LightFinder, you understand the risks of pairing with someone you don't know. If you are the person sending a request to someone with LightFinder on, you also understand the risks of pairing with someone you don't know. **AGAIN, THIS IS OPTIONAL.** * When in LightFinder mode, you can also put up a Syncshell you own on the Syncshell Finder so that others can easily find it and join. This has to be done prior to enabling LightFinder.  * Syncshell Finder allows you to join Syncshells that are indexed by LightFinder  # Minor Changes * Vanity addition to our supporters: On top of vanity ids you can find a fun addition under Your User Settings -> Edit Lightless Profile -> Vanity Settings to change the colour of your name in the lightless ui. This will be shown to all users.  * Pairing nameplate colour override can also now override FC tag (new option_ * Bunch of UI fixes, updates, and changes * kdb is now a whitelisted filetype that can upload Co-authored-by: CakeAndBanana <admin@cakeandbanana.nl> Co-authored-by: azyges <aaaaaa@aaa.aaa> Co-authored-by: choco <thijmenhogenkamp@gmail.com> Co-authored-by: choco <choco@noreply.git.lightless-sync.org> Co-authored-by: defnotken <itsdefnotken@gmail.com> Reviewed-on: #39
223 lines
7.3 KiB
C#
223 lines
7.3 KiB
C#
using Dalamud.Game.ClientState.Objects.SubKinds;
|
|
using Dalamud.Plugin.Services;
|
|
using LightlessSync.API.Dto.User;
|
|
using LightlessSync.LightlessConfiguration;
|
|
using LightlessSync.Services.Mediator;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace LightlessSync.Services;
|
|
|
|
public class BroadcastScannerService : DisposableMediatorSubscriberBase, IDisposable
|
|
{
|
|
private readonly ILogger<BroadcastScannerService> _logger;
|
|
private readonly IObjectTable _objectTable;
|
|
private readonly IFramework _framework;
|
|
|
|
private readonly BroadcastService _broadcastService;
|
|
private readonly NameplateHandler _nameplateHandler;
|
|
|
|
private readonly ConcurrentDictionary<string, BroadcastEntry> _broadcastCache = new();
|
|
private readonly Queue<string> _lookupQueue = new();
|
|
private readonly HashSet<string> _lookupQueuedCids = new();
|
|
private readonly HashSet<string> _syncshellCids = new();
|
|
|
|
private static readonly TimeSpan MaxAllowedTtl = TimeSpan.FromMinutes(4);
|
|
private static readonly TimeSpan RetryDelay = TimeSpan.FromMinutes(1);
|
|
|
|
private readonly CancellationTokenSource _cleanupCts = new();
|
|
private Task? _cleanupTask;
|
|
|
|
private int _checkEveryFrames = 20;
|
|
private int _frameCounter = 0;
|
|
private int _lookupsThisFrame = 0;
|
|
private const int MaxLookupsPerFrame = 30;
|
|
private const int MaxQueueSize = 100;
|
|
|
|
private volatile bool _batchRunning = false;
|
|
|
|
public IReadOnlyDictionary<string, BroadcastEntry> BroadcastCache => _broadcastCache;
|
|
public readonly record struct BroadcastEntry(bool IsBroadcasting, DateTime ExpiryTime, string? GID);
|
|
|
|
public BroadcastScannerService(ILogger<BroadcastScannerService> logger,
|
|
IClientState clientState,
|
|
IObjectTable objectTable,
|
|
IFramework framework,
|
|
BroadcastService broadcastService,
|
|
LightlessMediator mediator,
|
|
NameplateHandler nameplateHandler,
|
|
DalamudUtilService dalamudUtil,
|
|
LightlessConfigService configService) : base(logger, mediator)
|
|
{
|
|
_logger = logger;
|
|
_objectTable = objectTable;
|
|
_broadcastService = broadcastService;
|
|
_nameplateHandler = nameplateHandler;
|
|
|
|
_logger = logger;
|
|
_framework = framework;
|
|
_framework.Update += OnFrameworkUpdate;
|
|
|
|
Mediator.Subscribe<BroadcastStatusChangedMessage>(this, OnBroadcastStatusChanged);
|
|
_cleanupTask = Task.Run(ExpiredBroadcastCleanupLoop);
|
|
|
|
_nameplateHandler.Init();
|
|
}
|
|
|
|
private void OnFrameworkUpdate(IFramework framework) => Update();
|
|
|
|
public void Update()
|
|
{
|
|
_frameCounter++;
|
|
_lookupsThisFrame = 0;
|
|
|
|
if (!_broadcastService.IsBroadcasting)
|
|
return;
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
foreach (var obj in _objectTable)
|
|
{
|
|
if (obj is not IPlayerCharacter player || player.Address == IntPtr.Zero)
|
|
continue;
|
|
|
|
var cid = DalamudUtilService.GetHashedCIDFromPlayerPointer(player.Address);
|
|
var isStale = !_broadcastCache.TryGetValue(cid, out var entry) || entry.ExpiryTime <= now;
|
|
|
|
if (isStale && _lookupQueuedCids.Add(cid) && _lookupQueue.Count < MaxQueueSize)
|
|
_lookupQueue.Enqueue(cid);
|
|
}
|
|
|
|
if (_frameCounter % _checkEveryFrames == 0 && _lookupQueue.Count > 0)
|
|
{
|
|
var cidsToLookup = new List<string>();
|
|
while (_lookupQueue.Count > 0 && _lookupsThisFrame < MaxLookupsPerFrame)
|
|
{
|
|
var cid = _lookupQueue.Dequeue();
|
|
_lookupQueuedCids.Remove(cid);
|
|
cidsToLookup.Add(cid);
|
|
_lookupsThisFrame++;
|
|
}
|
|
|
|
if (cidsToLookup.Count > 0 && !_batchRunning)
|
|
{
|
|
_batchRunning = true;
|
|
_ = BatchUpdateBroadcastCacheAsync(cidsToLookup).ContinueWith(_ => _batchRunning = false);
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task BatchUpdateBroadcastCacheAsync(List<string> cids)
|
|
{
|
|
var results = await _broadcastService.AreUsersBroadcastingAsync(cids).ConfigureAwait(false);
|
|
var now = DateTime.UtcNow;
|
|
|
|
foreach (var (cid, info) in results)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(cid) || info == null)
|
|
continue;
|
|
|
|
var ttl = info.IsBroadcasting && info.TTL.HasValue
|
|
? TimeSpan.FromTicks(Math.Min(info.TTL.Value.Ticks, MaxAllowedTtl.Ticks))
|
|
: RetryDelay;
|
|
|
|
var expiry = now + ttl;
|
|
|
|
_broadcastCache.AddOrUpdate(cid,
|
|
new BroadcastEntry(info.IsBroadcasting, expiry, info.GID),
|
|
(_, old) => new BroadcastEntry(info.IsBroadcasting, expiry, info.GID));
|
|
}
|
|
|
|
var activeCids = _broadcastCache
|
|
.Where(e => e.Value.IsBroadcasting && e.Value.ExpiryTime > now)
|
|
.Select(e => e.Key)
|
|
.ToList();
|
|
|
|
_nameplateHandler.UpdateBroadcastingCids(activeCids);
|
|
UpdateSyncshellBroadcasts();
|
|
}
|
|
|
|
private void OnBroadcastStatusChanged(BroadcastStatusChangedMessage msg)
|
|
{
|
|
if (!msg.Enabled)
|
|
{
|
|
_broadcastCache.Clear();
|
|
_lookupQueue.Clear();
|
|
_lookupQueuedCids.Clear();
|
|
_syncshellCids.Clear();
|
|
|
|
_nameplateHandler.UpdateBroadcastingCids(Enumerable.Empty<string>());
|
|
}
|
|
}
|
|
|
|
private void UpdateSyncshellBroadcasts()
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
var newSet = _broadcastCache
|
|
.Where(e => e.Value.IsBroadcasting && e.Value.ExpiryTime > now && !string.IsNullOrEmpty(e.Value.GID))
|
|
.Select(e => e.Key)
|
|
.ToHashSet();
|
|
|
|
if (!_syncshellCids.SetEquals(newSet))
|
|
{
|
|
_syncshellCids.Clear();
|
|
foreach (var cid in newSet)
|
|
_syncshellCids.Add(cid);
|
|
|
|
Mediator.Publish(new SyncshellBroadcastsUpdatedMessage());
|
|
}
|
|
}
|
|
|
|
public List<BroadcastStatusInfoDto> GetActiveSyncshellBroadcasts()
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
|
|
return _broadcastCache
|
|
.Where(e => e.Value.IsBroadcasting && e.Value.ExpiryTime > now && !string.IsNullOrEmpty(e.Value.GID))
|
|
.Select(e => new BroadcastStatusInfoDto
|
|
{
|
|
HashedCID = e.Key,
|
|
IsBroadcasting = true,
|
|
TTL = e.Value.ExpiryTime - now,
|
|
GID = e.Value.GID
|
|
})
|
|
.ToList();
|
|
}
|
|
|
|
private async Task ExpiredBroadcastCleanupLoop()
|
|
{
|
|
var token = _cleanupCts.Token;
|
|
|
|
try
|
|
{
|
|
while (!token.IsCancellationRequested)
|
|
{
|
|
await Task.Delay(TimeSpan.FromSeconds(10), token);
|
|
|
|
var now = DateTime.UtcNow;
|
|
foreach (var (cid, entry) in _broadcastCache.ToArray())
|
|
{
|
|
if (entry.ExpiryTime <= now)
|
|
_broadcastCache.TryRemove(cid, out _);
|
|
}
|
|
}
|
|
}
|
|
catch (OperationCanceledException) { }
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Broadcast cleanup loop crashed");
|
|
}
|
|
|
|
UpdateSyncshellBroadcasts();
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
_framework.Update -= OnFrameworkUpdate;
|
|
_cleanupCts.Cancel();
|
|
_cleanupTask?.Wait(100);
|
|
_nameplateHandler.Uninit();
|
|
}
|
|
}
|