use try..finally to make sure rented arrays are returned
This commit is contained in:
parent
48d0f70ffd
commit
40d5cd7c05
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using StardewValley;
|
||||
|
@ -64,7 +63,8 @@ namespace StardewModdingAPI.Framework.Content
|
|||
{
|
||||
int pixelCount = areaWidth * areaHeight;
|
||||
sourceData = ArrayPool<Color>.Shared.Rent(pixelCount);
|
||||
|
||||
try
|
||||
{
|
||||
// slower copying, line by line
|
||||
for (int y = areaY, maxY = areaY + areaHeight; y < maxY; y++)
|
||||
{
|
||||
|
@ -75,12 +75,14 @@ namespace StardewModdingAPI.Framework.Content
|
|||
|
||||
// apply
|
||||
this.PatchImageImpl(sourceData, source.Width, source.Height, sourceArea.Value, targetArea.Value, patchMode);
|
||||
|
||||
// return
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<Color>.Shared.Return(sourceData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void PatchImage(Texture2D source, Rectangle? sourceArea = null, Rectangle? targetArea = null, PatchMode patchMode = PatchMode.Replace)
|
||||
|
@ -98,14 +100,16 @@ namespace StardewModdingAPI.Framework.Content
|
|||
// get source data
|
||||
int pixelCount = sourceArea.Value.Width * sourceArea.Value.Height;
|
||||
Color[] sourceData = ArrayPool<Color>.Shared.Rent(pixelCount);
|
||||
try
|
||||
{
|
||||
source.GetData(0, sourceArea, sourceData, 0, pixelCount);
|
||||
|
||||
// apply
|
||||
this.PatchImageImpl(sourceData, source.Width, source.Height, sourceArea.Value, targetArea.Value, patchMode);
|
||||
|
||||
// return
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<Color>.Shared.Return(sourceData);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool ExtendImage(int minWidth, int minHeight)
|
||||
|
@ -207,6 +211,8 @@ namespace StardewModdingAPI.Framework.Content
|
|||
|
||||
// get target data
|
||||
Color[] mergedData = ArrayPool<Color>.Shared.Rent(pixelCount);
|
||||
try
|
||||
{
|
||||
target.GetData(0, targetArea, mergedData, 0, pixelCount);
|
||||
|
||||
// merge pixels
|
||||
|
@ -241,8 +247,12 @@ namespace StardewModdingAPI.Framework.Content
|
|||
}
|
||||
|
||||
target.SetData(0, targetArea, mergedData, 0, pixelCount);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<Color>.Shared.Return(mergedData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -395,6 +395,8 @@ namespace StardewModdingAPI.Framework.ContentManagers
|
|||
// premultiply pixels
|
||||
int count = texture.Width * texture.Height;
|
||||
Color[] data = ArrayPool<Color>.Shared.Rent(count);
|
||||
try
|
||||
{
|
||||
texture.GetData(data, 0, count);
|
||||
|
||||
bool changed = false;
|
||||
|
@ -411,10 +413,13 @@ namespace StardewModdingAPI.Framework.ContentManagers
|
|||
if (changed)
|
||||
texture.SetData(data, 0, count);
|
||||
|
||||
// return
|
||||
ArrayPool<Color>.Shared.Return(data);
|
||||
return texture;
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<Color>.Shared.Return(data);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Fix custom map tilesheet paths so they can be found by the content manager.</summary>
|
||||
/// <param name="map">The map whose tilesheets to fix.</param>
|
||||
|
|
Loading…
Reference in New Issue