diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-10-08 17:42:32 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-10-08 17:42:32 -0400 |
commit | 40d5cd7c05d4e0a4e6894cd7b9f6d7d747716837 (patch) | |
tree | fa1419606c3dba97a7ad8e2132576589162f26a8 /src/SMAPI/Framework/ContentManagers | |
parent | 48d0f70ffd07df9ba364808a71407800834f95d3 (diff) | |
download | SMAPI-40d5cd7c05d4e0a4e6894cd7b9f6d7d747716837.tar.gz SMAPI-40d5cd7c05d4e0a4e6894cd7b9f6d7d747716837.tar.bz2 SMAPI-40d5cd7c05d4e0a4e6894cd7b9f6d7d747716837.zip |
use try..finally to make sure rented arrays are returned
Diffstat (limited to 'src/SMAPI/Framework/ContentManagers')
-rw-r--r-- | src/SMAPI/Framework/ContentManagers/ModContentManager.cs | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs index 0c793808..6b8a5874 100644 --- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs @@ -395,25 +395,30 @@ namespace StardewModdingAPI.Framework.ContentManagers // premultiply pixels int count = texture.Width * texture.Height; Color[] data = ArrayPool<Color>.Shared.Rent(count); - texture.GetData(data, 0, count); - - bool changed = false; - for (int i = 0; i < count; i++) + try { - ref Color pixel = ref data[i]; - if (pixel.A is (byte.MinValue or byte.MaxValue)) - continue; // no need to change fully transparent/opaque pixels + texture.GetData(data, 0, count); - data[i] = new Color(pixel.R * pixel.A / byte.MaxValue, pixel.G * pixel.A / byte.MaxValue, pixel.B * pixel.A / byte.MaxValue, pixel.A); // slower version: Color.FromNonPremultiplied(data[i].ToVector4()) - changed = true; - } + bool changed = false; + for (int i = 0; i < count; i++) + { + ref Color pixel = ref data[i]; + if (pixel.A is (byte.MinValue or byte.MaxValue)) + continue; // no need to change fully transparent/opaque pixels - if (changed) - texture.SetData(data, 0, count); + data[i] = new Color(pixel.R * pixel.A / byte.MaxValue, pixel.G * pixel.A / byte.MaxValue, pixel.B * pixel.A / byte.MaxValue, pixel.A); // slower version: Color.FromNonPremultiplied(data[i].ToVector4()) + changed = true; + } - // return - ArrayPool<Color>.Shared.Return(data); - return texture; + if (changed) + texture.SetData(data, 0, count); + + return texture; + } + finally + { + ArrayPool<Color>.Shared.Return(data); + } } /// <summary>Fix custom map tilesheet paths so they can be found by the content manager.</summary> |