Fix warning

This commit is contained in:
2025-10-30 22:13:38 +01:00
parent d891dceb28
commit b4108c7803
40 changed files with 378 additions and 193 deletions

View File

@@ -1,4 +1,5 @@
using Dalamud.Game.ClientState.Objects.Types;
using System;
using Dalamud.Plugin;
using Dalamud.Plugin.Ipc;
using MareSynchronos.MareConfiguration;
@@ -29,7 +30,7 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
private bool _marePluginEnabled = false;
private bool _impersonating = false;
private DateTime _unregisterTime = DateTime.UtcNow;
private CancellationTokenSource _registerDelayCts = new();
private CancellationTokenSource? _registerDelayCts = new();
public bool MarePluginEnabled => _marePluginEnabled;
public bool ImpersonationActive => _impersonating;
@@ -100,7 +101,7 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
{
if (_mareConfig.Current.MareAPI)
{
var cancelToken = _registerDelayCts.Token;
var cancelToken = EnsureFreshCts(ref _registerDelayCts).Token;
Task.Run(async () =>
{
// Wait before registering to reduce the chance of a race condition
@@ -125,7 +126,7 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
}
else
{
_registerDelayCts = _registerDelayCts.CancelRecreate();
EnsureFreshCts(ref _registerDelayCts);
if (_impersonating)
{
_loadFileProviderMare?.UnregisterFunc();
@@ -146,7 +147,7 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
_loadFileAsyncProvider?.UnregisterFunc();
_handledGameAddresses?.UnregisterFunc();
_registerDelayCts.Cancel();
TryCancel(_registerDelayCts);
if (_impersonating)
{
_loadFileProviderMare?.UnregisterFunc();
@@ -155,6 +156,7 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
}
Mediator.UnsubscribeAll(this);
CancelAndDispose(ref _registerDelayCts);
return Task.CompletedTask;
}
@@ -193,4 +195,31 @@ public class IpcProvider : IHostedService, IMediatorSubscriber
return _activeGameObjectHandlers.Where(g => g.Address != nint.Zero).Select(g => g.Address).Distinct().ToList();
}
private static CancellationTokenSource EnsureFreshCts(ref CancellationTokenSource? cts)
{
CancelAndDispose(ref cts);
cts = new CancellationTokenSource();
return cts;
}
private static void CancelAndDispose(ref CancellationTokenSource? cts)
{
if (cts == null) return;
TryCancel(cts);
cts.Dispose();
cts = null;
}
private static void TryCancel(CancellationTokenSource? cts)
{
if (cts == null) return;
try
{
cts.Cancel();
}
catch (ObjectDisposedException)
{
}
}
}

View File

@@ -1,19 +1,20 @@
using Dalamud.Game.ClientState.Objects.Types;
using System;
using MareSynchronos.PlayerData.Handlers;
using MareSynchronos.Services;
using MareSynchronos.Services.Mediator;
using MareSynchronos.Utils;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
namespace MareSynchronos.Interop.Ipc;
public class RedrawManager
public class RedrawManager : IDisposable
{
private readonly MareMediator _mareMediator;
private readonly DalamudUtilService _dalamudUtil;
private readonly ConcurrentDictionary<nint, bool> _penumbraRedrawRequests = [];
private CancellationTokenSource _disposalCts = new();
private CancellationTokenSource? _disposalCts = new();
private bool _disposed;
public SemaphoreSlim RedrawSemaphore { get; init; } = new(2, 2);
@@ -32,12 +33,12 @@ public class RedrawManager
try
{
using CancellationTokenSource cancelToken = new CancellationTokenSource();
using CancellationTokenSource combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancelToken.Token, token, _disposalCts.Token);
using CancellationTokenSource combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancelToken.Token, token, EnsureFreshCts(ref _disposalCts).Token);
var combinedToken = combinedCts.Token;
cancelToken.CancelAfter(TimeSpan.FromSeconds(15));
await handler.ActOnFrameworkAfterEnsureNoDrawAsync(action, combinedToken).ConfigureAwait(false);
if (!_disposalCts.Token.IsCancellationRequested)
if (!_disposalCts!.Token.IsCancellationRequested)
await _dalamudUtil.WaitWhileCharacterIsDrawing(logger, handler, applicationId, 30000, combinedToken).ConfigureAwait(false);
}
finally
@@ -49,6 +50,45 @@ public class RedrawManager
internal void Cancel()
{
_disposalCts = _disposalCts.CancelRecreate();
EnsureFreshCts(ref _disposalCts);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
CancelAndDispose(ref _disposalCts);
}
_disposed = true;
}
private static CancellationTokenSource EnsureFreshCts(ref CancellationTokenSource? cts)
{
CancelAndDispose(ref cts);
cts = new CancellationTokenSource();
return cts;
}
private static void CancelAndDispose(ref CancellationTokenSource? cts)
{
if (cts == null) return;
try
{
cts.Cancel();
}
catch (ObjectDisposedException)
{
}
cts.Dispose();
cts = null;
}
}