Skip to content

Commit

Permalink
Fixes for #35 (require with __mod-name__)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Bilka2 committed Dec 12, 2020
1 parent 69a19ea commit 89899e7
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions FactorioDataWrapper/src/com/demod/factorio/FactorioData.java
Expand Up @@ -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");
Expand Down Expand Up @@ -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> 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);
Expand All @@ -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> 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());
Expand Down

0 comments on commit 89899e7

Please sign in to comment.