3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +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.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; 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; import java.util.function.Consumer;
public class FileSystemUtils { public class FileSystemUtils {
@ -38,18 +40,20 @@ public class FileSystemUtils {
* path of the given {@link Class}. * path of the given {@link Class}.
* *
* @param target The target class of the resource path to scan * @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 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"}) @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 { throws IOException {
final File file = new File(target final File file = new File(target
.getProtectionDomain().getCodeSource().getLocation().getPath()); .getProtectionDomain().getCodeSource().getLocation().getPath());
if (file.isFile()) { // jar if (file.isFile()) { // jar
try (FileSystem fileSystem = FileSystems.newFileSystem(file.toPath(), (ClassLoader) null)) { try (FileSystem fileSystem = FileSystems.newFileSystem(file.toPath(), (ClassLoader) null)) {
Path toVisit = fileSystem.getPath(path); Path toVisit = fileSystem.getPath(firstPathComponent, remainingPathComponents);
if (Files.exists(toVisit)) { if (Files.exists(toVisit)) {
consumer.accept(toVisit); consumer.accept(toVisit);
return true; return true;
@ -58,8 +62,12 @@ public class FileSystemUtils {
} }
} else { } else {
URI uri; URI uri;
List<String> componentList = new ArrayList<>();
componentList.add(firstPathComponent);
componentList.addAll(Arrays.asList(remainingPathComponents));
try { try {
URL url = target.getClassLoader().getResource(path); URL url = target.getClassLoader().getResource(String.join("/", componentList));
if (url == null) { if (url == null) {
return false; return false;
} }