mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-14 20:54:16 +01:00
Cache DalamudAssetAttribute lookups (#2014)
This commit is contained in:
parent
ecf8e323c4
commit
7a45fb05e3
3 changed files with 37 additions and 8 deletions
|
|
@ -364,7 +364,7 @@ internal class ImGuiWidget : IDataWindowWidget
|
||||||
|
|
||||||
public static readonly string[] AssetSources =
|
public static readonly string[] AssetSources =
|
||||||
Enum.GetValues<DalamudAsset>()
|
Enum.GetValues<DalamudAsset>()
|
||||||
.Where(x => x.GetAttribute<DalamudAssetAttribute>()?.Purpose is DalamudAssetPurpose.TextureFromPng)
|
.Where(x => x.GetPurpose() is DalamudAssetPurpose.TextureFromPng)
|
||||||
.Select(Enum.GetName)
|
.Select(Enum.GetName)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
using Dalamud.Utility;
|
using System.Collections.Frozen;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using Dalamud.Utility;
|
||||||
|
|
||||||
namespace Dalamud.Storage.Assets;
|
namespace Dalamud.Storage.Assets;
|
||||||
|
|
||||||
|
|
@ -7,11 +10,37 @@ namespace Dalamud.Storage.Assets;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class DalamudAssetExtensions
|
public static class DalamudAssetExtensions
|
||||||
{
|
{
|
||||||
|
private static readonly FrozenDictionary<DalamudAsset, DalamudAssetAttribute> AttributeCache = CreateCache();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the purpose.
|
/// Gets the purpose.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="asset">The asset.</param>
|
/// <param name="asset">The asset.</param>
|
||||||
/// <returns>The purpose.</returns>
|
/// <returns>The purpose.</returns>
|
||||||
public static DalamudAssetPurpose GetPurpose(this DalamudAsset asset) =>
|
public static DalamudAssetPurpose GetPurpose(this DalamudAsset asset)
|
||||||
asset.GetAttribute<DalamudAssetAttribute>()?.Purpose ?? DalamudAssetPurpose.Empty;
|
{
|
||||||
|
return GetAssetAttribute(asset)?.Purpose ?? DalamudAssetPurpose.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the attribute.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="asset">The asset.</param>
|
||||||
|
/// <returns>The attribute.</returns>
|
||||||
|
internal static DalamudAssetAttribute? GetAssetAttribute(this DalamudAsset asset)
|
||||||
|
{
|
||||||
|
return AttributeCache.GetValueOrDefault(asset);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static FrozenDictionary<DalamudAsset, DalamudAssetAttribute> CreateCache()
|
||||||
|
{
|
||||||
|
var dict = new Dictionary<DalamudAsset, DalamudAssetAttribute>();
|
||||||
|
|
||||||
|
foreach (var asset in Enum.GetValues<DalamudAsset>())
|
||||||
|
{
|
||||||
|
dict.Add(asset, asset.GetAttribute<DalamudAssetAttribute>());
|
||||||
|
}
|
||||||
|
|
||||||
|
return dict.ToFrozenDictionary();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ internal sealed class DalamudAssetManager : IInternalDisposableService, IDalamud
|
||||||
Task.WhenAll(
|
Task.WhenAll(
|
||||||
Enum.GetValues<DalamudAsset>()
|
Enum.GetValues<DalamudAsset>()
|
||||||
.Where(x => x is not DalamudAsset.Empty4X4)
|
.Where(x => x is not DalamudAsset.Empty4X4)
|
||||||
.Where(x => x.GetAttribute<DalamudAssetAttribute>()?.Required is true)
|
.Where(x => x.GetAssetAttribute()?.Required is true)
|
||||||
.Select(this.CreateStreamAsync)
|
.Select(this.CreateStreamAsync)
|
||||||
.Select(x => x.ToContentDisposedTask()))
|
.Select(x => x.ToContentDisposedTask()))
|
||||||
.ContinueWith(
|
.ContinueWith(
|
||||||
|
|
@ -83,7 +83,7 @@ internal sealed class DalamudAssetManager : IInternalDisposableService, IDalamud
|
||||||
Task.WhenAll(
|
Task.WhenAll(
|
||||||
Enum.GetValues<DalamudAsset>()
|
Enum.GetValues<DalamudAsset>()
|
||||||
.Where(x => x is not DalamudAsset.Empty4X4)
|
.Where(x => x is not DalamudAsset.Empty4X4)
|
||||||
.Where(x => x.GetAttribute<DalamudAssetAttribute>()?.Required is false)
|
.Where(x => x.GetAssetAttribute()?.Required is false)
|
||||||
.Select(this.CreateStreamAsync)
|
.Select(this.CreateStreamAsync)
|
||||||
.Select(x => x.ToContentDisposedTask(true)))
|
.Select(x => x.ToContentDisposedTask(true)))
|
||||||
.ContinueWith(r => Log.Verbose($"Optional assets load state: {r}"));
|
.ContinueWith(r => Log.Verbose($"Optional assets load state: {r}"));
|
||||||
|
|
@ -120,7 +120,7 @@ internal sealed class DalamudAssetManager : IInternalDisposableService, IDalamud
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
[Pure]
|
[Pure]
|
||||||
public bool IsStreamImmediatelyAvailable(DalamudAsset asset) =>
|
public bool IsStreamImmediatelyAvailable(DalamudAsset asset) =>
|
||||||
asset.GetAttribute<DalamudAssetAttribute>()?.Data is not null
|
asset.GetAssetAttribute()?.Data is not null
|
||||||
|| this.fileStreams[asset]?.IsCompletedSuccessfully is true;
|
|| this.fileStreams[asset]?.IsCompletedSuccessfully is true;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
|
@ -140,7 +140,7 @@ internal sealed class DalamudAssetManager : IInternalDisposableService, IDalamud
|
||||||
[Pure]
|
[Pure]
|
||||||
public Task<Stream> CreateStreamAsync(DalamudAsset asset)
|
public Task<Stream> CreateStreamAsync(DalamudAsset asset)
|
||||||
{
|
{
|
||||||
if (asset.GetAttribute<DalamudAssetAttribute>() is { Data: { } rawData })
|
if (asset.GetAssetAttribute() is { Data: { } rawData })
|
||||||
return Task.FromResult<Stream>(new MemoryStream(rawData, false));
|
return Task.FromResult<Stream>(new MemoryStream(rawData, false));
|
||||||
|
|
||||||
Task<FileStream> task;
|
Task<FileStream> task;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue