Add state fetchers

This commit is contained in:
Soreepeong 2024-02-22 03:32:13 +09:00
parent eb4d9aba7e
commit 5eadfc1b4d
7 changed files with 72 additions and 20 deletions

View file

@ -0,0 +1,20 @@
using System.Diagnostics.CodeAnalysis;
namespace Dalamud.Interface.Internal;
/// <summary>
/// Represents a lookup for a game icon.
/// </summary>
/// <param name="IconId">The icon ID.</param>
/// <param name="ItemHq">Whether the HQ icon is requested, where HQ is in the context of items.</param>
/// <param name="HiRes">Whether the high-resolution icon is requested.</param>
/// <param name="Language">The language of the icon to load.</param>
[SuppressMessage(
"StyleCop.CSharp.NamingRules",
"SA1313:Parameter names should begin with lower-case letter",
Justification = "no")]
public record struct GameIconLookup(
uint IconId,
bool ItemHq = false,
bool HiRes = true,
ClientLanguage? Language = null);

View file

@ -21,9 +21,6 @@ internal sealed class FileSystemSharableTexture : SharableTexture
this.UnderlyingWrap = this.CreateTextureAsync();
}
/// <inheritdoc/>
protected override Task<IDalamudTextureWrap> UnderlyingWrap { get; set; }
/// <inheritdoc/>
public override string ToString() =>
$"{nameof(FileSystemSharableTexture)}#{this.InstanceIdForDebug}({this.path})";
@ -32,10 +29,8 @@ internal sealed class FileSystemSharableTexture : SharableTexture
protected override void FinalRelease()
{
this.DisposeSuppressingWrap = null;
_ = this.UnderlyingWrap.ToContentDisposedTask(true);
this.UnderlyingWrap =
Task.FromException<IDalamudTextureWrap>(new ObjectDisposedException(nameof(GamePathSharableTexture)));
_ = this.UnderlyingWrap.Exception;
_ = this.UnderlyingWrap?.ToContentDisposedTask(true);
this.UnderlyingWrap = null;
}
/// <inheritdoc/>

View file

@ -25,9 +25,6 @@ internal sealed class GamePathSharableTexture : SharableTexture
this.UnderlyingWrap = this.CreateTextureAsync();
}
/// <inheritdoc/>
protected override Task<IDalamudTextureWrap> UnderlyingWrap { get; set; }
/// <inheritdoc/>
public override string ToString() => $"{nameof(GamePathSharableTexture)}#{this.InstanceIdForDebug}({this.path})";
@ -35,10 +32,8 @@ internal sealed class GamePathSharableTexture : SharableTexture
protected override void FinalRelease()
{
this.DisposeSuppressingWrap = null;
_ = this.UnderlyingWrap.ToContentDisposedTask(true);
this.UnderlyingWrap =
Task.FromException<IDalamudTextureWrap>(new ObjectDisposedException(nameof(GamePathSharableTexture)));
_ = this.UnderlyingWrap.Exception;
_ = this.UnderlyingWrap?.ToContentDisposedTask(true);
this.UnderlyingWrap = null;
}
/// <inheritdoc/>

View file

@ -66,7 +66,7 @@ internal abstract class SharableTexture : IRefCountable
/// <summary>
/// Gets or sets the underlying texture wrap.
/// </summary>
protected abstract Task<IDalamudTextureWrap> UnderlyingWrap { get; set; }
public Task<IDalamudTextureWrap>? UnderlyingWrap { get; set; }
/// <summary>
/// Gets or sets the dispose-suppressing wrap for <see cref="UnderlyingWrap"/>.
@ -185,8 +185,18 @@ internal abstract class SharableTexture : IRefCountable
public Task<IDalamudTextureWrap> CreateNewReference()
{
this.AddRef();
if (this.UnderlyingWrap is null)
throw new InvalidOperationException("AddRef returned but UnderlyingWrap is null?");
return this.UnderlyingWrap.ContinueWith(
r => (IDalamudTextureWrap)new RefCountableWrappingTextureWrap(r.Result, this));
r =>
{
if (r.IsCompletedSuccessfully)
return Task.FromResult((IDalamudTextureWrap)new RefCountableWrappingTextureWrap(r.Result, this));
this.Release();
return r;
}).Unwrap();
}
/// <summary>
@ -207,9 +217,12 @@ internal abstract class SharableTexture : IRefCountable
if (this.RevivalPossibility?.TryGetTarget(out this.availableOnAccessWrapForApi9) is true)
return this.availableOnAccessWrapForApi9;
this.UnderlyingWrap.Wait();
if (this.UnderlyingWrap.Exception is not null)
var newRefTask = this.CreateNewReference();
newRefTask.Wait();
if (!newRefTask.IsCompletedSuccessfully)
return null;
newRefTask.Result.Dispose();
this.availableOnAccessWrapForApi9 = new AvailableOnAccessTextureWrap(this);
this.RevivalPossibility = new(this.availableOnAccessWrapForApi9);
}
@ -326,7 +339,7 @@ internal abstract class SharableTexture : IRefCountable
if (this.inner.GetImmediate() is { } t)
return t;
this.inner.UnderlyingWrap.Wait();
this.inner.UnderlyingWrap?.Wait();
return this.inner.DisposeSuppressingWrap ?? Service<DalamudAssetManager>.Get().Empty4X4;
}
}

View file

@ -145,6 +145,36 @@ internal sealed class TextureManager : IServiceType, IDisposable, ITextureProvid
this.fileSystemTextures.GetOrAdd(file.FullName, CreateFileSystemSharableTexture).GetAvailableOnAccessWrapForApi9();
#pragma warning restore CS0618 // Type or member is obsolete
/// <inheritdoc/>
public bool ImmediateGetStateFromGameIcon(in GameIconLookup lookup, out Exception? exception) =>
this.ImmediateGetStateFromGame(this.lookupToPath.GetOrAdd(lookup, this.GetIconPathByValue), out exception);
/// <inheritdoc/>
public bool ImmediateGetStateFromGame(string path, out Exception? exception)
{
if (!this.gamePathTextures.TryGetValue(path, out var texture))
{
exception = null;
return false;
}
exception = texture.UnderlyingWrap?.Exception;
return texture.UnderlyingWrap?.IsCompletedSuccessfully ?? false;
}
/// <inheritdoc/>
public bool ImmediateGetStateFromFile(string file, out Exception? exception)
{
if (!this.fileSystemTextures.TryGetValue(file, out var texture))
{
exception = null;
return false;
}
exception = texture.UnderlyingWrap?.Exception;
return texture.UnderlyingWrap?.IsCompletedSuccessfully ?? false;
}
/// <inheritdoc/>
public IDalamudTextureWrap ImmediateGetFromGameIcon(in GameIconLookup lookup) =>
this.ImmediateGetFromGame(this.lookupToPath.GetOrAdd(lookup, this.GetIconPathByValue));