Skip to content

Commit 1964f4c

Browse files
committed
Minimize fileio for raw images
1 parent 7dae07a commit 1964f4c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/SMAPI/Framework/ContentManagers/ModContentManager.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Buffers;
3+
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
56
using System.Globalization;
@@ -45,6 +46,8 @@ internal sealed class ModContentManager : BaseContentManager
4546
/// <summary>If a map tilesheet's image source has no file extensions, the file extensions to check for in the local mod folder.</summary>
4647
private static readonly string[] LocalTilesheetExtensions = [".png", ".xnb"];
4748

49+
private readonly Dictionary<string, RawTextureData> TextureCache = [];
50+
4851

4952
/*********
5053
** Public methods
@@ -220,6 +223,12 @@ private T LoadImageFile<T>(IAssetName assetName, FileInfo file)
220223
[SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "The 'forRawData' parameter is only added for mods which may intercept this method.")]
221224
private IRawTextureData LoadRawImageData(FileInfo file, bool forRawData)
222225
{
226+
if (this.TextureCache.TryGetValue(file.FullName, out var cacheResult))
227+
{
228+
var cacheColors = cacheResult.Data;
229+
return new RawTextureData(cacheResult.Width, cacheResult.Height, cacheColors);
230+
}
231+
223232
// load raw data
224233
int width;
225234
int height;
@@ -238,13 +247,19 @@ private IRawTextureData LoadRawImageData(FileInfo file, bool forRawData)
238247

239248
// convert to XNA pixel format
240249
var pixels = GC.AllocateUninitializedArray<Color>(rawPixels.Length);
250+
var pixelCache = GC.AllocateUninitializedArray<Color>(rawPixels.Length);
241251
for (int i = 0; i < pixels.Length; i++)
242252
{
243253
SKPMColor pixel = rawPixels[i];
244254
pixels[i] = pixel.Alpha == 0
245255
? Color.Transparent
246256
: new Color(r: pixel.Red, g: pixel.Green, b: pixel.Blue, alpha: pixel.Alpha);
257+
258+
pixelCache[i] = pixel.Alpha == 0
259+
? Color.Transparent
260+
: new Color(r: pixel.Red, g: pixel.Green, b: pixel.Blue, alpha: pixel.Alpha);
247261
}
262+
this.TextureCache.Add(file.FullName, new RawTextureData(width, height, pixelCache));
248263

249264
return new RawTextureData(width, height, pixels);
250265
}

0 commit comments

Comments
 (0)