Fix some IResources being leaked (#4806)

This commit is contained in:
ichttt authored and mezz committed 2018-03-19 22:58:54 -07:00
1 parent 881aa67be7
commit cf43250584
5 files changed
+66 -46

No files matched your search

@@ -61,6 +61,8 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import net.minecraftforge.fml.common.FMLLog;
import org.apache.commons.io.IOUtils;
public final class ModelDynBucket implements IModel
{
@@ -346,25 +348,31 @@ public final class ModelDynBucket implements IModel
final int[][] pixels = new int[Minecraft.getMinecraft().gameSettings.mipmapLevels + 1][];
pixels[0] = new int[width * height];
IResource empty = getResource(new ResourceLocation("textures/items/bucket_empty.png"));
IResource mask = getResource(new ResourceLocation(ForgeVersion.MOD_ID, "textures/items/vanilla_bucket_cover_mask.png"));
// use the alpha mask if it fits, otherwise leave the cover texture blank
if (empty != null && mask != null && Objects.equals(empty.getResourcePackName(), mask.getResourcePackName()) &&
alphaMask.getIconWidth() == width && alphaMask.getIconHeight() == height)
{
final int[][] oldPixels = sprite.getFrameTextureData(0);
final int[][] alphaPixels = alphaMask.getFrameTextureData(0);
for (int p = 0; p < width * height; p++)
try (
IResource empty = getResource(new ResourceLocation("textures/items/bucket_empty.png"));
IResource mask = getResource(new ResourceLocation(ForgeVersion.MOD_ID, "textures/items/vanilla_bucket_cover_mask.png"))
) {
// use the alpha mask if it fits, otherwise leave the cover texture blank
if (empty != null && mask != null && Objects.equals(empty.getResourcePackName(), mask.getResourcePackName()) &&
alphaMask.getIconWidth() == width && alphaMask.getIconHeight() == height)
{
final int alphaMultiplier = alphaPixels[0][p] >>> 24;
final int oldPixel = oldPixels[0][p];
final int oldPixelAlpha = oldPixel >>> 24;
final int newAlpha = oldPixelAlpha * alphaMultiplier / 0xFF;
pixels[0][p] = (oldPixel & 0xFFFFFF) + (newAlpha << 24);
final int[][] oldPixels = sprite.getFrameTextureData(0);
final int[][] alphaPixels = alphaMask.getFrameTextureData(0);
for (int p = 0; p < width * height; p++)
{
final int alphaMultiplier = alphaPixels[0][p] >>> 24;
final int oldPixel = oldPixels[0][p];
final int oldPixelAlpha = oldPixel >>> 24;
final int newAlpha = oldPixelAlpha * alphaMultiplier / 0xFF;
pixels[0][p] = (oldPixel & 0xFFFFFF) + (newAlpha << 24);
}
}
}
catch (IOException e)
{
FMLLog.log.error("Failed to close resource", e);
}
this.clearFramesTextureData();
this.framesTextureData.add(pixels);
@@ -35,6 +35,7 @@ import javax.vecmath.Matrix4f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Level;
import net.minecraft.client.renderer.block.model.BlockPart;
@@ -554,19 +555,17 @@ public class ModelBlockAnimation
{
try
{
IResource resource = null;
try
try (IResource resource = manager.getResource(armatureLocation))
{
resource = manager.getResource(armatureLocation);
ModelBlockAnimation mba = mbaGson.fromJson(new InputStreamReader(resource.getInputStream(), "UTF-8"), ModelBlockAnimation.class);
//String json = mbaGson.toJson(mba);
return mba;
}
catch(FileNotFoundException e)
{
// this is normal. FIXME: error reporting?
return defaultModelBlockAnimation;
}
ModelBlockAnimation mba = mbaGson.fromJson(new InputStreamReader(resource.getInputStream(), "UTF-8"), ModelBlockAnimation.class);
//String json = mbaGson.toJson(mba);
return mba;
}
catch(IOException | JsonParseException e)
{
@@ -70,6 +70,7 @@ import net.minecraftforge.common.property.IUnlistedProperty;
import net.minecraftforge.common.property.Properties;
import net.minecraftforge.fml.common.FMLLog;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
@@ -126,9 +127,9 @@ public enum B3DLoader implements ICustomModelLoader
ResourceLocation file = new ResourceLocation(modelLocation.getResourceDomain(), modelLocation.getResourcePath());
if(!cache.containsKey(file))
{
IResource resource = null;
try
{
IResource resource;
try
{
resource = manager.getResource(file);
@@ -150,6 +151,10 @@ public enum B3DLoader implements ICustomModelLoader
cache.put(file, null);
throw e;
}
finally
{
IOUtils.closeQuietly(resource);
}
}
B3DModel model = cache.get(file);
if(model == null) throw new ModelLoaderRegistry.LoaderException("Error loading model previously: " + file);
@@ -33,6 +33,7 @@ import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.fml.common.FMLLog;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Level;
/*
@@ -74,32 +75,39 @@ public enum OBJLoader implements ICustomModelLoader {
ResourceLocation file = new ResourceLocation(modelLocation.getResourceDomain(), modelLocation.getResourcePath());
if (!cache.containsKey(file))
{
IResource resource;
IResource resource = null;
try
{
resource = manager.getResource(file);
}
catch (FileNotFoundException e)
{
if (modelLocation.getResourcePath().startsWith("models/block/"))
resource = manager.getResource(new ResourceLocation(file.getResourceDomain(), "models/item/" + file.getResourcePath().substring("models/block/".length())));
else if (modelLocation.getResourcePath().startsWith("models/item/"))
resource = manager.getResource(new ResourceLocation(file.getResourceDomain(), "models/block/" + file.getResourcePath().substring("models/item/".length())));
else throw e;
}
OBJModel.Parser parser = new OBJModel.Parser(resource, manager);
OBJModel model = null;
try
{
model = parser.parse();
}
catch (Exception e)
{
errors.put(modelLocation, e);
try
{
resource = manager.getResource(file);
}
catch (FileNotFoundException e)
{
if (modelLocation.getResourcePath().startsWith("models/block/"))
resource = manager.getResource(new ResourceLocation(file.getResourceDomain(), "models/item/" + file.getResourcePath().substring("models/block/".length())));
else if (modelLocation.getResourcePath().startsWith("models/item/"))
resource = manager.getResource(new ResourceLocation(file.getResourceDomain(), "models/block/" + file.getResourcePath().substring("models/item/".length())));
else throw e;
}
OBJModel.Parser parser = new OBJModel.Parser(resource, manager);
OBJModel model = null;
try
{
model = parser.parse();
}
catch (Exception e)
{
errors.put(modelLocation, e);
}
finally
{
cache.put(modelLocation, model);
}
}
finally
{
cache.put(modelLocation, model);
IOUtils.closeQuietly(resource);
}
}
OBJModel model = cache.get(file);
@@ -51,6 +51,7 @@ import net.minecraftforge.common.util.JsonUtils;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
@@ -215,13 +216,12 @@ public final class AnimationStateMachine implements IAnimationStateMachine
@SideOnly(Side.CLIENT)
public static IAnimationStateMachine load(IResourceManager manager, ResourceLocation location, ImmutableMap<String, ITimeValue> customParameters)
{
try
try (IResource resource = manager.getResource(location))
{
ClipResolver clipResolver = new ClipResolver();
ParameterResolver parameterResolver = new ParameterResolver(customParameters);
Clips.CommonClipTypeAdapterFactory.INSTANCE.setClipResolver(clipResolver);
TimeValues.CommonTimeValueTypeAdapterFactory.INSTANCE.setValueResolver(parameterResolver);
IResource resource = manager.getResource(location);
AnimationStateMachine asm = asmGson.fromJson(new InputStreamReader(resource.getInputStream(), "UTF-8"), AnimationStateMachine.class);
clipResolver.asm = asm;
parameterResolver.asm = asm;