throw more cancellationtokens around

This commit is contained in:
rootdarkarchon
2023-03-15 00:32:09 +01:00
parent 22e11cac6a
commit 3134b2c7a8
2 changed files with 54 additions and 41 deletions

View File

@@ -607,18 +607,24 @@ public sealed class IpcManager : DisposableMediatorSubscriberBase
ActionQueue.Enqueue(action); ActionQueue.Enqueue(action);
if (!fireAndForget) try
{ {
var disposeToken = _disposalCts.Token; if (!fireAndForget)
var combinedToken = CancellationTokenSource.CreateLinkedTokenSource(disposeToken, token).Token; {
var disposeToken = _disposalCts.Token;
var combinedToken = CancellationTokenSource.CreateLinkedTokenSource(disposeToken, token).Token;
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false); await Task.Delay(TimeSpan.FromSeconds(1), combinedToken).ConfigureAwait(false);
if (!combinedToken.IsCancellationRequested)
await _dalamudUtil.WaitWhileCharacterIsDrawing(logger, obj, applicationId, 30000, combinedToken).ConfigureAwait(false);
if (!combinedToken.IsCancellationRequested)
await _dalamudUtil.WaitWhileCharacterIsDrawing(logger, obj, applicationId, 30000, combinedToken).ConfigureAwait(false);
}
}
finally
{
_penumbraRedrawRequests[obj.Address] = false; _penumbraRedrawRequests[obj.Address] = false;
} }
Mediator.Publish(new PenumbraEndRedrawMessage(obj.Address)); Mediator.Publish(new PenumbraEndRedrawMessage(obj.Address));
} }

View File

@@ -193,6 +193,7 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase
{ {
if (PlayerCharacter == IntPtr.Zero) return; if (PlayerCharacter == IntPtr.Zero) return;
var ptr = PlayerCharacter; var ptr = PlayerCharacter;
var handler = changes.Key switch var handler = changes.Key switch
{ {
ObjectKind.Player => _charaHandler!, ObjectKind.Player => _charaHandler!,
@@ -202,45 +203,51 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase
_ => throw new NotSupportedException("ObjectKind not supported: " + changes.Key) _ => throw new NotSupportedException("ObjectKind not supported: " + changes.Key)
}; };
if (handler.Address == IntPtr.Zero) try
{ {
if (handler != _charaHandler) handler.Dispose(); if (handler.Address == IntPtr.Zero)
return;
}
Logger.LogDebug("[{applicationId}] Applying Customization Data for {handler}", applicationId, handler);
await _dalamudUtil.WaitWhileCharacterIsDrawing(Logger, handler, applicationId, 30000).ConfigureAwait(false);
foreach (var change in changes.Value)
{
Logger.LogDebug("[{applicationId}] Processing {change} for {handler}", applicationId, change, handler);
switch (change)
{ {
case PlayerChanges.Palette: return;
await _ipcManager.PalettePlusSetPalette(handler.Address, charaData.PalettePlusData).ConfigureAwait(false); }
break;
case PlayerChanges.Customize: Logger.LogDebug("[{applicationId}] Applying Customization Data for {handler}", applicationId, handler);
await _ipcManager.CustomizePlusSetBodyScale(handler.Address, charaData.CustomizePlusData).ConfigureAwait(false); await _dalamudUtil.WaitWhileCharacterIsDrawing(Logger, handler, applicationId, 30000, token).ConfigureAwait(false);
break; token.ThrowIfCancellationRequested();
foreach (var change in changes.Value)
{
Logger.LogDebug("[{applicationId}] Processing {change} for {handler}", applicationId, change, handler);
switch (change)
{
case PlayerChanges.Palette:
await _ipcManager.PalettePlusSetPalette(handler.Address, charaData.PalettePlusData).ConfigureAwait(false);
break;
case PlayerChanges.Heels: case PlayerChanges.Customize:
await _ipcManager.HeelsSetOffsetForPlayer(handler.Address, charaData.HeelsOffset).ConfigureAwait(false); await _ipcManager.CustomizePlusSetBodyScale(handler.Address, charaData.CustomizePlusData).ConfigureAwait(false);
break; break;
case PlayerChanges.Mods: case PlayerChanges.Heels:
if (charaData.GlamourerData.TryGetValue(changes.Key, out var glamourerData)) await _ipcManager.HeelsSetOffsetForPlayer(handler.Address, charaData.HeelsOffset).ConfigureAwait(false);
{ break;
await _ipcManager.GlamourerApplyAll(Logger, handler, glamourerData, applicationId, token).ConfigureAwait(false);
} case PlayerChanges.Mods:
else if (charaData.GlamourerData.TryGetValue(changes.Key, out var glamourerData))
{ {
await _ipcManager.PenumbraRedraw(Logger, handler, applicationId, token).ConfigureAwait(false); await _ipcManager.GlamourerApplyAll(Logger, handler, glamourerData, applicationId, token).ConfigureAwait(false);
} }
break; else
{
await _ipcManager.PenumbraRedraw(Logger, handler, applicationId, token).ConfigureAwait(false);
}
break;
}
token.ThrowIfCancellationRequested();
} }
} }
finally
if (handler != _charaHandler) handler.Dispose(); {
if (handler != _charaHandler) handler.Dispose();
}
} }
private Dictionary<ObjectKind, HashSet<PlayerChanges>> CheckUpdatedData(CharacterData oldData, CharacterData newData, bool forced) private Dictionary<ObjectKind, HashSet<PlayerChanges>> CheckUpdatedData(CharacterData oldData, CharacterData newData, bool forced)
@@ -382,14 +389,14 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase
} }
} }
while ((!_applicationTask?.IsCompleted ?? false) && !downloadToken.IsCancellationRequested) while ((!_applicationTask?.IsCompleted ?? false) && !downloadToken.IsCancellationRequested && !_applicationCancellationTokenSource.IsCancellationRequested)
{ {
// block until current application is done // block until current application is done
Logger.LogDebug("Waiting for current data application (Id: {id}) to finish", _applicationId); Logger.LogDebug("Waiting for current data application (Id: {id}) to finish", _applicationId);
await Task.Delay(250).ConfigureAwait(false); await Task.Delay(250).ConfigureAwait(false);
} }
if (downloadToken.IsCancellationRequested) return; if (downloadToken.IsCancellationRequested || _applicationCancellationTokenSource.IsCancellationRequested) return;
_applicationCancellationTokenSource?.Dispose(); _applicationCancellationTokenSource?.Dispose();
_applicationCancellationTokenSource = new(); _applicationCancellationTokenSource = new();