Skip to content

Commit

Permalink
Fix crash from CraftingHelper due to FileSystem being closed early
Browse files Browse the repository at this point in the history
  • Loading branch information
tterrag1098 committed Dec 2, 2018
1 parent 2a5268c commit 5ef199f
Showing 1 changed file with 63 additions and 48 deletions.
111 changes: 63 additions & 48 deletions src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand Down Expand Up @@ -626,16 +627,16 @@ else if (revertFrozen)

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())
{
Expand All @@ -655,6 +656,10 @@ else if (mod.getSource().isDirectory())
{
e.printStackTrace();
}
finally
{
IOUtils.closeQuietly(fs);
}
}

private static boolean loadRecipes(ModContainer mod)
Expand Down Expand Up @@ -756,63 +761,73 @@ public static boolean findFiles(ModContainer mod, String base, Function<Path, Bo
}
}

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<Path> itr = null;
try
Path root = null;

if (source.isFile())
{
itr = Files.walk(root).iterator();
try
{
fs = FileSystems.newFileSystem(source.toPath(), null);
root = fs.getPath("/" + base);
}
catch (IOException e)
{
FMLLog.log.error("Error loading FileSystem from jar: ", e);
return false;
}
}
catch (IOException e)
else if (source.isDirectory())
{
FMLLog.log.error("Error iterating filesystem for: {}", mod.getModId(), e);
return false;
root = source.toPath().resolve(base);
}

while (itr != null && itr.hasNext())

if (root == null || !Files.exists(root))
return defaultUnfoundRoot;

if (preprocessor != null)
{
Boolean cont = processor.apply(root, itr.next());

if (visitAllFiles)
Boolean cont = preprocessor.apply(root);
if (cont == null || !cont.booleanValue())
return false;
}

if (processor != null)
{
Iterator<Path> itr = null;
try
{
success &= cont != null && cont;
itr = Files.walk(root).iterator();
}
else if (cont == null || !cont)
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;
}
Expand Down

0 comments on commit 5ef199f

Please sign in to comment.