Skip to content

Commit

Permalink
Added invalid file name detector for assets and data folders
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Oct 20, 2023
1 parent 5ed0bd6 commit b7249e7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/src/main/java/dev/latvian/mods/kubejs/KubeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import dev.latvian.mods.kubejs.script.ScriptPack;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.ScriptsLoadedEvent;
import dev.latvian.mods.kubejs.script.data.GeneratedResourcePack;
import dev.latvian.mods.kubejs.server.KubeJSServerEventHandler;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import dev.latvian.mods.kubejs.util.KubeJSBackgroundThread;
Expand Down Expand Up @@ -166,6 +167,9 @@ public KubeJS() throws Throwable {
event.created.forEach(BuilderBase::createAdditionalObjects);
}
}

GeneratedResourcePack.scanForInvalidFiles("kubejs/assets/", KubeJSPaths.ASSETS);
GeneratedResourcePack.scanForInvalidFiles("kubejs/data/", KubeJSPaths.DATA);
}

public static void loadScripts(ScriptPack pack, Path dir, String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.latvian.mods.kubejs.DevProperties;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import dev.latvian.mods.kubejs.util.Lazy;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.AbstractPackResources;
Expand All @@ -23,8 +24,47 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;

public abstract class GeneratedResourcePack implements ExportablePackResources {
private static Stream<Path> tryWalk(Path path) {
try {
return Files.walk(path);
} catch (Exception ignore) {
}

return Stream.empty();
}

public static void scanForInvalidFiles(String pathName, Path path) throws IOException {
for (var p : Files.list(path).filter(Files::isDirectory).flatMap(GeneratedResourcePack::tryWalk).filter(Files::isRegularFile).filter(Files::isReadable).toList()) {
try {
var fileName = p.getFileName().toString();

if (fileName.endsWith(".zip") || fileName.equals(".ds_store") || fileName.equals("thumbs.db") || fileName.equals("desktop.ini")) {
return;
} else if (Files.isHidden(path)) {
ConsoleJS.STARTUP.error("Invisible file found: " + pathName + path.relativize(p).toString().replace('\\', '/'));
return;
}

var chars = fileName.toCharArray();

for (char c : chars) {
if (c >= 'A' && c <= 'Z') {
ConsoleJS.STARTUP.error("Invalid file name: Uppercase '" + c + "' in " + pathName + path.relativize(p).toString().replace('\\', '/'));
return;
} else if (c != '_' && c != '-' && (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '/' && c != '.') {
ConsoleJS.STARTUP.error("Invalid file name: Invalid character '" + c + "' in " + pathName + path.relativize(p).toString().replace('\\', '/'));
return;
}
}
} catch (Exception ex) {
ConsoleJS.STARTUP.error("Invalid file name: " + pathName + path.relativize(p).toString().replace('\\', '/'));
}
}
}

private final PackType packType;
private Map<ResourceLocation, GeneratedData> generated;
private Set<String> generatedNamespaces;
Expand Down

0 comments on commit b7249e7

Please sign in to comment.