Fix crash from CraftingHelper due to FileSystem being closed early

This commit is contained in:
tterrag committed 2018-12-01 23:33:22 -05:00
1 parent 2a5268c699
commit 5ef199f6d7
1 file changed
+66 -51
@@ -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<Path> 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<Path> 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;