From 89899e739d5a226d5ab7d55afea8e77cf47abf5a Mon Sep 17 00:00:00 2001 From: Bilka Date: Sat, 12 Dec 2020 18:26:50 +0100 Subject: [PATCH] Fixes for #35 (require with __mod-name__) Don't take precedence over the other patterns(__MOD__, __RESOURCE__); error when mod does not exist; work with paths separated with "\\" (and ".", which is converted to "\\" by LuaJ) --- .../src/com/demod/factorio/FactorioData.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/FactorioDataWrapper/src/com/demod/factorio/FactorioData.java b/FactorioDataWrapper/src/com/demod/factorio/FactorioData.java index b24d262..cd3814b 100644 --- a/FactorioDataWrapper/src/com/demod/factorio/FactorioData.java +++ b/FactorioDataWrapper/src/com/demod/factorio/FactorioData.java @@ -205,9 +205,6 @@ private static DataTable initializeDataTable() throws JSONException, IOException File[] luaFolders = new File[] { // new File(factorio, "data/core/lualib"), // - // new File(factorio, "data"), // - // new File(factorio, "data/core"), // - // new File(factorio, "data/base"), // }; JSONArray modExcludeJson = Config.get().optJSONArray("mod-exclude"); @@ -241,19 +238,6 @@ private static DataTable initializeDataTable() throws JSONException, IOException globals.finder = new ResourceFinder() { @Override public InputStream findResource(String filename) { - String firstSegment = filename.split("\\/")[0]; - if (firstSegment.startsWith("__") && firstSegment.endsWith("__")) { - String modName = firstSegment.substring(2, firstSegment.length() - 2); - Optional mod = modLoader.getMod(modName); - if(mod.isPresent()) { - try { - return mod.get().getResource(filename.replace(firstSegment, "")).orElse(null); - } catch (Exception e) { - e.printStackTrace(); - throw new InternalError(e); - } - } - } if (filename.startsWith(SEARCH_MOD) && currentMod.get() != null) { try { return currentMod.get().getResource(filename.replace(SEARCH_MOD, "")).orElse(null); @@ -266,6 +250,19 @@ public InputStream findResource(String filename) { .getResourceAsStream(filename.replace(SEARCH_RESOURCE, "lua")); // System.out.println(stream != null); return stream; + } else if (filename.startsWith("__") && (filename.indexOf("__", 2) > -1)) { + int matchEnd = filename.indexOf("__", 2); + String modName = filename.substring(2, matchEnd); + Optional mod = modLoader.getMod(modName); + if (!mod.isPresent()) { + throw new IllegalStateException("Mod does not exist: " + modName); + } + try { + return mod.get().getResource(filename.substring(matchEnd + 2)).orElse(null); + } catch (Exception e) { + e.printStackTrace(); + throw new InternalError(e); + } } else { File file = new File(filename); // System.out.println(file.exists());