3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-16 21:10:30 +01:00

Try #2 at explicit path

Dieser Commit ist enthalten in:
Andrew Steinborn 2021-04-19 08:01:58 -04:00
Ursprung 6a349d24ac
Commit 7c0b622742

Datei anzeigen

@ -28,7 +28,9 @@ import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class FileSystemUtils {
@ -38,18 +40,20 @@ public class FileSystemUtils {
* path of the given {@link Class}.
*
* @param target The target class of the resource path to scan
* @param path The path to scan within the resource path
* @param consumer The consumer to visit the resolved path
* @param firstPathComponent First path component
* @param remainingPathComponents Remaining path components
*/
@SuppressFBWarnings({"RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"})
public static boolean visitResources(Class<?> target, String path, Consumer<Path> consumer)
public static boolean visitResources(Class<?> target, Consumer<Path> consumer,
String firstPathComponent, String... remainingPathComponents)
throws IOException {
final File file = new File(target
.getProtectionDomain().getCodeSource().getLocation().getPath());
if (file.isFile()) { // jar
try (FileSystem fileSystem = FileSystems.newFileSystem(file.toPath(), (ClassLoader) null)) {
Path toVisit = fileSystem.getPath(path);
Path toVisit = fileSystem.getPath(firstPathComponent, remainingPathComponents);
if (Files.exists(toVisit)) {
consumer.accept(toVisit);
return true;
@ -58,8 +62,12 @@ public class FileSystemUtils {
}
} else {
URI uri;
List<String> componentList = new ArrayList<>();
componentList.add(firstPathComponent);
componentList.addAll(Arrays.asList(remainingPathComponents));
try {
URL url = target.getClassLoader().getResource(path);
URL url = target.getClassLoader().getResource(String.join("/", componentList));
if (url == null) {
return false;
}