mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-29 20:03:41 +01:00
Add ITextureProvider.GetFromManifestResource(Assembly,string)
This commit is contained in:
parent
b52d4724e9
commit
cc756c243c
5 changed files with 327 additions and 35 deletions
|
|
@ -0,0 +1,64 @@
|
|||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dalamud.Utility;
|
||||
|
||||
namespace Dalamud.Interface.Internal.SharedImmediateTextures;
|
||||
|
||||
/// <summary>Represents a sharable texture, based on a manifest texture obtained from
|
||||
/// <see cref="Assembly.GetManifestResourceStream(string)"/>.</summary>
|
||||
internal sealed class ManifestResourceSharedImmediateTexture : SharedImmediateTexture
|
||||
{
|
||||
private readonly Assembly assembly;
|
||||
private readonly string name;
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="ManifestResourceSharedImmediateTexture"/> class.</summary>
|
||||
/// <param name="assembly">The assembly containing manifest resources.</param>
|
||||
/// <param name="name">The case-sensitive name of the manifest resource being requested.</param>
|
||||
private ManifestResourceSharedImmediateTexture(Assembly assembly, string name)
|
||||
{
|
||||
this.assembly = assembly;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string SourcePathForDebug => $"{this.assembly.GetName().FullName}:{this.name}";
|
||||
|
||||
/// <summary>Creates a new placeholder instance of <see cref="ManifestResourceSharedImmediateTexture"/>.</summary>
|
||||
/// <param name="args">The arguments to pass to the constructor.</param>
|
||||
/// <returns>The new instance.</returns>
|
||||
public static SharedImmediateTexture CreatePlaceholder((Assembly Assembly, string Name) args) =>
|
||||
new ManifestResourceSharedImmediateTexture(args.Assembly, args.Name);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToString() =>
|
||||
$"{nameof(ManifestResourceSharedImmediateTexture)}#{this.InstanceIdForDebug}({this.SourcePathForDebug})";
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void ReleaseResources()
|
||||
{
|
||||
_ = this.UnderlyingWrap?.ToContentDisposedTask(true);
|
||||
this.UnderlyingWrap = null;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void ReviveResources() =>
|
||||
this.UnderlyingWrap = Service<TextureLoadThrottler>.Get().CreateLoader(
|
||||
this,
|
||||
this.CreateTextureAsync,
|
||||
this.LoadCancellationToken);
|
||||
|
||||
private async Task<IDalamudTextureWrap> CreateTextureAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await using var stream = this.assembly.GetManifestResourceStream(this.name);
|
||||
if (stream is null)
|
||||
throw new FileNotFoundException("The resource file could not be found.");
|
||||
|
||||
var tm = await Service<TextureManager>.GetAsync();
|
||||
var ms = new MemoryStream(stream.CanSeek ? (int)stream.Length : 0);
|
||||
await stream.CopyToAsync(ms, cancellationToken);
|
||||
return tm.NoThrottleGetFromImage(ms.GetBuffer().AsMemory(0, (int)ms.Length));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue