Skip to content

Commit

Permalink
Strip the directory name from mod contents, without assuming that the…
Browse files Browse the repository at this point in the history
… directory name exactly matches the mod name. (demodude4u#43)

Co-authored-by: Bilka <[email protected]>
  • Loading branch information
FactorioBlueprints and Bilka2 authored Dec 31, 2022
1 parent b6dac4e commit f5c9381
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions FactorioDataWrapper/src/com/demod/factorio/ModLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.json.JSONException;

Expand Down Expand Up @@ -58,18 +56,16 @@ public Optional<InputStream> getResource(String path) throws FileNotFoundExcepti
}
}
}

public static class ModZip implements Mod {
private final Map<String, byte[]> files = new LinkedHashMap<>();
private ModInfo info;

private volatile Optional<String> lastResourceFolder = Optional.empty();

public ModZip(File file) throws FileNotFoundException, IOException {
String prefix = file.getName().substring(0, file.getName().length() - 4);
try (ZipFile zipFile = new ZipFile(file)) {
zipFile.stream().forEach(entry -> {
String name = entry.getName().replace(prefix, "");
String name = stripDirectoryName(entry.getName());

try (InputStream inputStream = zipFile.getInputStream(entry)) {
files.put(name, ByteStreams.toByteArray(inputStream));
Expand All @@ -89,6 +85,14 @@ public ModZip(File file) throws FileNotFoundException, IOException {
}
}

private static String stripDirectoryName(String name) {
int firstSlash = name.indexOf('/');
if (firstSlash == -1) {
return name;
}
return name.substring(firstSlash);
}

@Override
public ModInfo getInfo() {
return info;
Expand Down

0 comments on commit f5c9381

Please sign in to comment.