Add methods to allow loading json constants outside of _constants (#4975)

* add interface methods for loading json constants from an arbitary file

* use try-with-resources

* don't make modders create jsoncontext, clean up resource use

* very minor cleanup
This commit is contained in:
temp1011 authored and tterrag committed 2018-12-01 19:46:19 -05:00
1 parent 7f337cf230
commit 5f56b7cd09
4 files changed
+224 -111

No files matched your search

@@ -21,6 +21,7 @@ package net.minecraftforge.common.crafting;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -28,7 +29,6 @@ import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -42,8 +42,6 @@ import java.util.function.Function;
import javax.annotation.Nonnull;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Level;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@@ -628,37 +626,35 @@ public class CraftingHelper {
private static void loadFactories(ModContainer mod)
{
FileSystem fs = null;
BufferedReader reader = null;
Path fPath = null;
JsonContext ctx = new JsonContext(mod.getModId());
try
{
JsonContext ctx = new JsonContext(mod.getModId());
Path fPath = null;
if (mod.getSource().isFile())
{
fs = FileSystems.newFileSystem(mod.getSource().toPath(), null);
fPath = fs.getPath("/assets/" + ctx.getModId() + "/recipes/_factories.json");
try (FileSystem fs = FileSystems.newFileSystem(mod.getSource().toPath(), null))
{
fPath = fs.getPath("/assets/" + ctx.getModId() + "/recipes/_factories.json");
}
}
else if (mod.getSource().isDirectory())
{
fPath = mod.getSource().toPath().resolve("assets/" + ctx.getModId() + "/recipes/_factories.json");
}
if (fPath != null && Files.exists(fPath))
{
reader = Files.newBufferedReader(fPath);
JsonObject json = JsonUtils.fromJson(GSON, reader, JsonObject.class);
loadFactories(json, ctx);
try (BufferedReader reader = Files.newBufferedReader(fPath))
{
JsonObject json = JsonUtils.fromJson(GSON, reader, JsonObject.class);
loadFactories(json, ctx);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
IOUtils.closeQuietly(fs);
IOUtils.closeQuietly(reader);
}
}
private static boolean loadRecipes(ModContainer mod)
@@ -671,10 +667,8 @@ public class CraftingHelper {
Path fPath = root.resolve("_constants.json");
if (fPath != null && Files.exists(fPath))
{
BufferedReader reader = null;
try
try(BufferedReader reader = Files.newBufferedReader(fPath))
{
reader = Files.newBufferedReader(fPath);
JsonObject[] json = JsonUtils.fromJson(GSON, reader, JsonObject[].class);
ctx.loadConstants(json);
}
@@ -683,10 +677,6 @@ public class CraftingHelper {
FMLLog.log.error("Error loading _constants.json: ", e);
return false;
}
finally
{
IOUtils.closeQuietly(reader);
}
}
return true;
},
@@ -701,10 +691,8 @@ public class CraftingHelper {
String name = FilenameUtils.removeExtension(relative).replaceAll("\\\\", "/");
ResourceLocation key = new ResourceLocation(ctx.getModId(), name);
BufferedReader reader = null;
try
try(BufferedReader reader = Files.newBufferedReader(file))
{
reader = Files.newBufferedReader(file);
JsonObject json = JsonUtils.fromJson(GSON, reader, JsonObject.class);
if (json.has("conditions") && !CraftingHelper.processConditions(JsonUtils.getJsonArray(json, "conditions"), ctx))
return true;
@@ -721,10 +709,6 @@ public class CraftingHelper {
FMLLog.log.error("Couldn't read recipe {} from {}", key, file, e);
return false;
}
finally
{
IOUtils.closeQuietly(reader);
}
return true;
},
true, true
@@ -749,94 +733,145 @@ public class CraftingHelper {
return findFiles(mod, base, preprocessor, processor, defaultUnfoundRoot, false);
}
public static boolean findFiles(ModContainer mod, String base, Function<Path, Boolean> preprocessor, BiFunction<Path, Path, Boolean> processor, boolean defaultUnfoundRoot, boolean visitAllFiles)
public static boolean findFiles(ModContainer mod, String base, Function<Path, Boolean> preprocessor, BiFunction<Path, Path, Boolean> processor,
boolean defaultUnfoundRoot, boolean visitAllFiles)
{
FileSystem fs = null;
try
File source = mod.getSource();
if ("minecraft".equals(mod.getModId()))
{
File source = mod.getSource();
if (!DEBUG_LOAD_MINECRAFT)
return true;
if ("minecraft".equals(mod.getModId()))
try
{
if (!DEBUG_LOAD_MINECRAFT)
return true;
try
{
URI tmp = CraftingManager.class.getResource("/assets/.mcassetsroot").toURI();
source = new File(tmp.resolve("..").getPath());
}
catch (URISyntaxException e)
{
FMLLog.log.error("Error finding Minecraft jar: ", e);
return false;
}
URI tmp = CraftingManager.class.getResource("/assets/.mcassetsroot").toURI();
source = new File(tmp.resolve("..").getPath());
}
Path root = null;
if (source.isFile())
catch (URISyntaxException e)
{
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;
}
FMLLog.log.error("Error finding Minecraft 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;
}
boolean success = true;
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;
}
}
}
return success;
}
finally
Path root = null;
if (source.isFile())
{
IOUtils.closeQuietly(fs);
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;
}
boolean success = true;
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;
}
}
}
return success;
}
public static JsonContext loadContext(File file) throws IOException
{
ModContainer mod = Loader.instance().activeModContainer();
if(mod == null)
{
throw new RuntimeException("Error loading constants, no mod to load for found");
}
return loadContext(new JsonContext(mod.getModId()), file);
}
public static JsonContext loadContext(String path) throws IOException
{
ModContainer mod = Loader.instance().activeModContainer();
if(mod == null)
{
throw new RuntimeException("Error loading constants, no mod to load for found");
}
return loadContext(mod, new JsonContext(mod.getModId()), path);
}
private static JsonContext loadContext(JsonContext ctx, File file) throws IOException
{
try(BufferedReader reader = new BufferedReader(new FileReader(file)))
{
JsonObject[] json = JsonUtils.fromJson(GSON, reader, JsonObject[].class);
ctx.loadConstants(json);
return ctx;
}
catch (IOException e)
{
FMLLog.log.error("Error loading constants from file: {}", file.getAbsolutePath(), e);
throw e;
}
}
private static JsonContext loadContext(ModContainer mod, JsonContext ctx, String path) throws IOException
{
Path fPath = null;
if(mod.getSource().isFile())
{
try(FileSystem fs = FileSystems.newFileSystem(mod.getSource().toPath(), null))
{
fPath = fs.getPath("/assets/" + ctx.getModId() + path);
}
}
else if (mod.getSource().isDirectory())
{
fPath = mod.getSource().toPath().resolve("assets/" + ctx.getModId() + path);
}
if (fPath != null && Files.exists(fPath))
{
return loadContext(ctx, fPath.toFile());
} else {
throw new IOException("path could not be resolved: " + path);
}
}
}
@@ -58,7 +58,7 @@ public class JsonContext
return constants.get(name);
}
void loadConstants(JsonObject[] jsons)
void loadConstants(JsonObject... jsons)
{
for (JsonObject json : jsons)
{