From 5ef199f6d71f2baba34fd4cc4ac3fa160274d580 Mon Sep 17 00:00:00 2001 From: tterrag Date: Sat, 1 Dec 2018 23:33:22 -0500 Subject: [PATCH] Fix crash from CraftingHelper due to FileSystem being closed early --- .../common/crafting/CraftingHelper.java | 117 ++++++++++-------- 1 file changed, 66 insertions(+), 51 deletions(-) diff --git a/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java b/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java index ced93b14d9..3b4396fe13 100644 --- a/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java +++ b/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java @@ -42,6 +42,7 @@ import java.util.function.Function; import javax.annotation.Nonnull; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -626,16 +627,16 @@ public class CraftingHelper { private static void loadFactories(ModContainer mod) { - Path fPath = null; - JsonContext ctx = new JsonContext(mod.getModId()); + FileSystem fs = null; try { + Path fPath = null; + JsonContext ctx = new JsonContext(mod.getModId()); + if (mod.getSource().isFile()) { - try (FileSystem fs = FileSystems.newFileSystem(mod.getSource().toPath(), null)) - { - fPath = fs.getPath("/assets/" + ctx.getModId() + "/recipes/_factories.json"); - } + fs = FileSystems.newFileSystem(mod.getSource().toPath(), null); + fPath = fs.getPath("/assets/" + ctx.getModId() + "/recipes/_factories.json"); } else if (mod.getSource().isDirectory()) { @@ -655,6 +656,10 @@ public class CraftingHelper { { e.printStackTrace(); } + finally + { + IOUtils.closeQuietly(fs); + } } private static boolean loadRecipes(ModContainer mod) @@ -756,62 +761,72 @@ public class CraftingHelper { } } - Path root = null; - if (source.isFile()) - { - try (FileSystem fs = FileSystems.newFileSystem(source.toPath(), null);) - { - root = fs.getPath("/" + base); - } - catch (IOException e) - { - FMLLog.log.error("Error loading FileSystem from jar: ", e); - return false; - } - } - else if (source.isDirectory()) - { - root = source.toPath().resolve(base); - } - - if (root == null || !Files.exists(root)) - return defaultUnfoundRoot; - - if (preprocessor != null) - { - Boolean cont = preprocessor.apply(root); - if (cont == null || !cont.booleanValue()) - return false; - } - + FileSystem fs = null; boolean success = true; - if (processor != null) + try { - Iterator itr = null; - try - { - itr = Files.walk(root).iterator(); - } - catch (IOException e) - { - FMLLog.log.error("Error iterating filesystem for: {}", mod.getModId(), e); - return false; - } + Path root = null; - while (itr != null && itr.hasNext()) + if (source.isFile()) { - Boolean cont = processor.apply(root, itr.next()); - - if (visitAllFiles) + try { - success &= cont != null && cont; + fs = FileSystems.newFileSystem(source.toPath(), null); + root = fs.getPath("/" + base); } - else if (cont == null || !cont) + catch (IOException e) { + FMLLog.log.error("Error loading FileSystem from jar: ", e); return false; } } + else if (source.isDirectory()) + { + root = source.toPath().resolve(base); + } + + if (root == null || !Files.exists(root)) + return defaultUnfoundRoot; + + if (preprocessor != null) + { + Boolean cont = preprocessor.apply(root); + if (cont == null || !cont.booleanValue()) + return false; + } + + if (processor != null) + { + Iterator itr = null; + try + { + itr = Files.walk(root).iterator(); + } + catch (IOException e) + { + FMLLog.log.error("Error iterating filesystem for: {}", mod.getModId(), e); + return false; + } + + while (itr != null && itr.hasNext()) + { + Boolean cont = processor.apply(root, itr.next()); + + if (visitAllFiles) + { + success &= cont != null && cont; + } + else if (cont == null || !cont) + { + return false; + } + } + } + } + finally + { + IOUtils.closeQuietly(fs); } return success;