From 248c7911a0ce2339a61cedf45430e1d29f3a6d23 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Thu, 22 Feb 2024 17:05:17 +0900 Subject: [PATCH] Ensure leaveOpen takes effect even on cancellations --- Dalamud/Interface/Internal/TextureManager.cs | 54 +++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/Dalamud/Interface/Internal/TextureManager.cs b/Dalamud/Interface/Internal/TextureManager.cs index 462a5ec76..378697e88 100644 --- a/Dalamud/Interface/Internal/TextureManager.cs +++ b/Dalamud/Interface/Internal/TextureManager.cs @@ -252,15 +252,23 @@ internal sealed class TextureManager : IServiceType, IDisposable, ITextureProvid bool leaveOpen = false, CancellationToken cancellationToken = default) => this.textureLoadThrottler.CreateLoader( - new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(), - async ct => - { - await using var streamDispose = leaveOpen ? null : stream; - await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new(); - await stream.CopyToAsync(ms, ct).ConfigureAwait(false); - return await this.GetFromImageAsync(ms.GetBuffer(), ct); - }, - cancellationToken); + new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(), + async ct => + { + await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new(); + await stream.CopyToAsync(ms, ct).ConfigureAwait(false); + return await this.GetFromImageAsync(ms.GetBuffer(), ct); + }, + cancellationToken) + .ContinueWith( + r => + { + if (!leaveOpen) + stream.Dispose(); + return r; + }, + default(CancellationToken)) + .Unwrap(); /// public IDalamudTextureWrap GetFromRaw( @@ -280,7 +288,7 @@ internal sealed class TextureManager : IServiceType, IDisposable, ITextureProvid CancellationToken cancellationToken = default) => this.textureLoadThrottler.CreateLoader( new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(), - ct => Task.FromResult(this.GetFromRaw(specs, bytes.Span)), + _ => Task.FromResult(this.GetFromRaw(specs, bytes.Span)), cancellationToken); /// @@ -290,15 +298,23 @@ internal sealed class TextureManager : IServiceType, IDisposable, ITextureProvid bool leaveOpen = false, CancellationToken cancellationToken = default) => this.textureLoadThrottler.CreateLoader( - new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(), - async ct => - { - await using var streamDispose = leaveOpen ? null : stream; - await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new(); - await stream.CopyToAsync(ms, ct).ConfigureAwait(false); - return await this.GetFromRawAsync(specs, ms.GetBuffer(), ct); - }, - cancellationToken); + new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(), + async ct => + { + await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new(); + await stream.CopyToAsync(ms, ct).ConfigureAwait(false); + return await this.GetFromRawAsync(specs, ms.GetBuffer(), ct); + }, + cancellationToken) + .ContinueWith( + r => + { + if (!leaveOpen) + stream.Dispose(); + return r; + }, + default(CancellationToken)) + .Unwrap(); /// public IDalamudTextureWrap GetTexture(TexFile file) => this.GetFromTexFileAsync(file).Result;