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

@ -1,6 +1,6 @@
using System.Diagnostics.CodeAnalysis;
namespace Dalamud.Plugin.Services;
namespace Dalamud.Interface.Internal;
/// <summary>
/// Represents a lookup for a game icon.

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));

View file

@ -1,5 +1,6 @@
using System.IO;
using Dalamud.Interface;
using Dalamud.Interface.Internal;
using Dalamud.Utility;

View file

@ -13,6 +13,24 @@ namespace Dalamud.Plugin.Services;
/// </summary>
public partial interface ITextureProvider
{
/// <summary>Gets the state of the background load task for <see cref="ImmediateGetFromGameIcon"/>.</summary>
/// <param name="lookup">The icon specifier.</param>
/// <param name="exception">The exception, if failed.</param>
/// <returns><c>true</c> if loaded; <c>false</c> if not fully loaded or failed.</returns>
public bool ImmediateGetStateFromGameIcon(in GameIconLookup lookup, out Exception? exception);
/// <summary>Gets the state of the background load task for <see cref="ImmediateGetFromGameIcon"/>.</summary>
/// <param name="path">The game-internal path to a .tex, .atex, or an image file such as .png.</param>
/// <param name="exception">The exception, if failed.</param>
/// <returns><c>true</c> if loaded; <c>false</c> if not fully loaded or failed.</returns>
public bool ImmediateGetStateFromGame(string path, out Exception? exception);
/// <summary>Gets the state of the background load task for <see cref="ImmediateGetFromGameIcon"/>.</summary>
/// <param name="file">The filesystem path to a .tex, .atex, or an image file such as .png.</param>
/// <param name="exception">The exception, if failed.</param>
/// <returns><c>true</c> if loaded; <c>false</c> if not fully loaded or failed.</returns>
public bool ImmediateGetStateFromFile(string file, out Exception? exception);
/// <summary>Gets the corresponding game icon for use with the current frame.</summary>
/// <param name="lookup">The icon specifier.</param>
/// <returns>An instance of <see cref="IDalamudTextureWrap"/> that is guaranteed to be available for the current