mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-01-03 06:13:40 +01:00
Ensure leaveOpen takes effect even on cancellations
This commit is contained in:
parent
3bf907002f
commit
248c7911a0
1 changed files with 35 additions and 19 deletions
|
|
@ -252,15 +252,23 @@ internal sealed class TextureManager : IServiceType, IDisposable, ITextureProvid
|
||||||
bool leaveOpen = false,
|
bool leaveOpen = false,
|
||||||
CancellationToken cancellationToken = default) =>
|
CancellationToken cancellationToken = default) =>
|
||||||
this.textureLoadThrottler.CreateLoader(
|
this.textureLoadThrottler.CreateLoader(
|
||||||
new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(),
|
new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(),
|
||||||
async ct =>
|
async ct =>
|
||||||
{
|
{
|
||||||
await using var streamDispose = leaveOpen ? null : stream;
|
await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new();
|
||||||
await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new();
|
await stream.CopyToAsync(ms, ct).ConfigureAwait(false);
|
||||||
await stream.CopyToAsync(ms, ct).ConfigureAwait(false);
|
return await this.GetFromImageAsync(ms.GetBuffer(), ct);
|
||||||
return await this.GetFromImageAsync(ms.GetBuffer(), ct);
|
},
|
||||||
},
|
cancellationToken)
|
||||||
cancellationToken);
|
.ContinueWith(
|
||||||
|
r =>
|
||||||
|
{
|
||||||
|
if (!leaveOpen)
|
||||||
|
stream.Dispose();
|
||||||
|
return r;
|
||||||
|
},
|
||||||
|
default(CancellationToken))
|
||||||
|
.Unwrap();
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public IDalamudTextureWrap GetFromRaw(
|
public IDalamudTextureWrap GetFromRaw(
|
||||||
|
|
@ -280,7 +288,7 @@ internal sealed class TextureManager : IServiceType, IDisposable, ITextureProvid
|
||||||
CancellationToken cancellationToken = default) =>
|
CancellationToken cancellationToken = default) =>
|
||||||
this.textureLoadThrottler.CreateLoader(
|
this.textureLoadThrottler.CreateLoader(
|
||||||
new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(),
|
new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(),
|
||||||
ct => Task.FromResult(this.GetFromRaw(specs, bytes.Span)),
|
_ => Task.FromResult(this.GetFromRaw(specs, bytes.Span)),
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
|
@ -290,15 +298,23 @@ internal sealed class TextureManager : IServiceType, IDisposable, ITextureProvid
|
||||||
bool leaveOpen = false,
|
bool leaveOpen = false,
|
||||||
CancellationToken cancellationToken = default) =>
|
CancellationToken cancellationToken = default) =>
|
||||||
this.textureLoadThrottler.CreateLoader(
|
this.textureLoadThrottler.CreateLoader(
|
||||||
new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(),
|
new TextureLoadThrottler.ReadOnlyThrottleBasisProvider(),
|
||||||
async ct =>
|
async ct =>
|
||||||
{
|
{
|
||||||
await using var streamDispose = leaveOpen ? null : stream;
|
await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new();
|
||||||
await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new();
|
await stream.CopyToAsync(ms, ct).ConfigureAwait(false);
|
||||||
await stream.CopyToAsync(ms, ct).ConfigureAwait(false);
|
return await this.GetFromRawAsync(specs, ms.GetBuffer(), ct);
|
||||||
return await this.GetFromRawAsync(specs, ms.GetBuffer(), ct);
|
},
|
||||||
},
|
cancellationToken)
|
||||||
cancellationToken);
|
.ContinueWith(
|
||||||
|
r =>
|
||||||
|
{
|
||||||
|
if (!leaveOpen)
|
||||||
|
stream.Dispose();
|
||||||
|
return r;
|
||||||
|
},
|
||||||
|
default(CancellationToken))
|
||||||
|
.Unwrap();
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public IDalamudTextureWrap GetTexture(TexFile file) => this.GetFromTexFileAsync(file).Result;
|
public IDalamudTextureWrap GetTexture(TexFile file) => this.GetFromTexFileAsync(file).Result;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue