add ITextureProvider.TryGetFromGameIcon()

This commit is contained in:
goat 2024-07-01 14:36:51 +02:00
parent f7cf037b9b
commit f574f797a1
3 changed files with 58 additions and 1 deletions

View file

@ -19,6 +19,20 @@ internal sealed partial class TextureManager
ISharedImmediateTexture ITextureProvider.GetFromGameIcon(in GameIconLookup lookup) =>
this.Shared.GetFromGameIcon(lookup);
/// <inheritdoc/>
bool ITextureProvider.TryGetFromGameIcon(
in GameIconLookup lookup, [NotNullWhen(true)] out ISharedImmediateTexture? texture)
{
if (this.Shared.TryGetFromGameIcon(lookup, out var pureImpl))
{
texture = pureImpl;
return true;
}
texture = null;
return false;
}
/// <inheritdoc/>
ISharedImmediateTexture ITextureProvider.GetFromGame(string path) =>
this.Shared.GetFromGame(path);
@ -94,6 +108,24 @@ internal sealed partial class TextureManager
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public SharedImmediateTexture.PureImpl GetFromGameIcon(in GameIconLookup lookup) =>
this.GetFromGame(this.lookupCache.GetOrAdd(lookup, this.GetIconPathByValue));
/// <inheritdoc cref="ITextureProvider.TryGetFromGameIcon"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetFromGameIcon(in GameIconLookup lookup, [NotNullWhen(true)] out SharedImmediateTexture.PureImpl? texture)
{
texture = null;
if (!this.lookupCache.TryGet(lookup, out var path))
{
if (!this.textureManager.TryGetIconPath(lookup, out path))
return false;
this.lookupCache.AddOrUpdate(lookup, path);
}
texture = this.GetFromGame(path);
return texture != null;
}
/// <inheritdoc cref="ITextureProvider.GetFromGame"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
@ -274,6 +275,20 @@ internal sealed class TextureManagerPluginScoped
return shared;
}
/// <inheritdoc/>
public bool TryGetFromGameIcon(in GameIconLookup lookup, [NotNullWhen(true)] out ISharedImmediateTexture? texture)
{
if (this.ManagerOrThrow.Shared.TryGetFromGameIcon(lookup, out var shared))
{
shared.AddOwnerPlugin(this.plugin);
texture = shared;
return true;
}
texture = null;
return false;
}
/// <inheritdoc/>
public ISharedImmediateTexture GetFromGame(string path)
{

View file

@ -189,10 +189,20 @@ public interface ITextureProvider
/// <returns>The shared texture that you may use to obtain the loaded texture wrap and load states.</returns>
/// <remarks>
/// <para>This function is under the effect of <see cref="ITextureSubstitutionProvider.GetSubstitutedPath"/>.</para>
/// <para>This function does not throw exceptions.</para>
/// <para>Caching the returned object is not recommended. Performance benefit will be minimal.</para>
/// </remarks>
ISharedImmediateTexture GetFromGameIcon(in GameIconLookup lookup);
/// <summary>Gets a shared texture corresponding to the given game resource icon specifier.</summary>
/// <remarks>
/// <para>This function does not throw exceptions.</para>
/// <para>This function is under the effect of <see cref="ITextureSubstitutionProvider.GetSubstitutedPath"/>.</para>
/// <para>Caching the returned object is not recommended. Performance benefit will be minimal.</para>
/// </remarks>
/// <param name="lookup">A game icon specifier.</param>
/// <param name="texture">The resulting <see cref="ISharedImmediateTexture"/>.</param>
/// <returns>Whether or not the lookup succeeded.</returns>
bool TryGetFromGameIcon(in GameIconLookup lookup, [NotNullWhen(true)] out ISharedImmediateTexture? texture);
/// <summary>Gets a shared texture corresponding to the given path to a game resource.</summary>
/// <param name="path">A path to a game resource.</param>