Skip to content

Commit

Permalink
Add methods to allow loading json constants outside of _constants (#4975
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
temp1011 authored and tterrag1098 committed Dec 2, 2018
1 parent 7f337cf commit 5f56b7c
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 94 deletions.
221 changes: 128 additions & 93 deletions src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
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;
Expand All @@ -42,8 +42,6 @@
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;
Expand Down Expand Up @@ -628,37 +626,35 @@ else if (revertFrozen)

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)
Expand All @@ -671,10 +667,8 @@ private static boolean loadRecipes(ModContainer mod)
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);
}
Expand All @@ -683,10 +677,6 @@ private static boolean loadRecipes(ModContainer mod)
FMLLog.log.error("Error loading _constants.json: ", e);
return false;
}
finally
{
IOUtils.closeQuietly(reader);
}
}
return true;
},
Expand All @@ -701,10 +691,8 @@ private static boolean loadRecipes(ModContainer mod)
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;
Expand All @@ -721,10 +709,6 @@ private static boolean loadRecipes(ModContainer mod)
FMLLog.log.error("Couldn't read recipe {} from {}", key, file, e);
return false;
}
finally
{
IOUtils.closeQuietly(reader);
}
return true;
},
true, true
Expand All @@ -749,94 +733,145 @@ public static boolean findFiles(ModContainer mod, String base, Function<Path, Bo
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());
}
catch (URISyntaxException e)
{
FMLLog.log.error("Error finding Minecraft jar: ", e);
return false;
}
}

Path root = null;
if (source.isFile())
Path root = null;
if (source.isFile())
{
try (FileSystem fs = FileSystems.newFileSystem(source.toPath(), null);)
{
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;
}
root = fs.getPath("/" + base);
}
else if (source.isDirectory())
catch (IOException e)
{
root = source.toPath().resolve(base);
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 (root == null || !Files.exists(root))
return defaultUnfoundRoot;
boolean success = true;

if (preprocessor != null)
if (processor != null)
{
Iterator<Path> itr = null;
try
{
Boolean cont = preprocessor.apply(root);
if (cont == null || !cont.booleanValue())
return false;
itr = Files.walk(root).iterator();
}
catch (IOException e)
{
FMLLog.log.error("Error iterating filesystem for: {}", mod.getModId(), e);
return false;
}

boolean success = true;

if (processor != null)
while (itr != null && itr.hasNext())
{
Iterator<Path> itr = null;
try
Boolean cont = processor.apply(root, itr.next());

if (visitAllFiles)
{
itr = Files.walk(root).iterator();
success &= cont != null && cont;
}
catch (IOException e)
else if (cont == null || !cont)
{
FMLLog.log.error("Error iterating filesystem for: {}", mod.getModId(), e);
return false;
}
}
}

while (itr != null && itr.hasNext())
{
Boolean cont = processor.apply(root, itr.next());
return success;
}

if (visitAllFiles)
{
success &= cont != null && cont;
}
else if (cont == null || !cont)
{
return false;
}
}
}
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;
}
}

return success;
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);
}
}
finally
else if (mod.getSource().isDirectory())
{
fPath = mod.getSource().toPath().resolve("assets/" + ctx.getModId() + path);
}

if (fPath != null && Files.exists(fPath))
{
IOUtils.closeQuietly(fs);
return loadContext(ctx, fPath.toFile());
} else {
throw new IOException("path could not be resolved: " + path);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Ingredient getConstant(String name)
return constants.get(name);
}

void loadConstants(JsonObject[] jsons)
void loadConstants(JsonObject... jsons)
{
for (JsonObject json : jsons)
{
Expand Down

0 comments on commit 5f56b7c

Please sign in to comment.