From 688babb4da94bcca95faf45521f927e928f2cf01 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Mon, 19 Apr 2021 08:48:42 -0400 Subject: [PATCH] Actually fix loading locales from paths with spaces in them --- .../proxy/util/FileSystemUtils.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/FileSystemUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/util/FileSystemUtils.java index 1139f004f..591308bcb 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/FileSystemUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/FileSystemUtils.java @@ -18,7 +18,6 @@ package com.velocitypowered.proxy.util; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -31,6 +30,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.function.Consumer; public class FileSystemUtils { @@ -48,11 +48,18 @@ public class FileSystemUtils { public static boolean visitResources(Class target, Consumer consumer, String firstPathComponent, String... remainingPathComponents) throws IOException { - final File file = new File(target - .getProtectionDomain().getCodeSource().getLocation().getPath()); + final URL knownResource = FileSystemUtils.class.getClassLoader() + .getResource("default-velocity.toml"); + if (knownResource == null) { + throw new IllegalStateException( + "default-velocity.toml does not exist, don't know where we are"); + } + if (knownResource.getProtocol().equals("jar")) { + // Running from a JAR + String jarPathRaw = knownResource.toString().split("!")[0]; + URI path = URI.create(jarPathRaw + "!/"); - if (file.isFile()) { // jar - try (FileSystem fileSystem = FileSystems.newFileSystem(file.toPath(), (ClassLoader) null)) { + try (FileSystem fileSystem = FileSystems.newFileSystem(path, Map.of("create", "true"))) { Path toVisit = fileSystem.getPath(firstPathComponent, remainingPathComponents); if (Files.exists(toVisit)) { consumer.accept(toVisit); @@ -61,6 +68,7 @@ public class FileSystemUtils { return false; } } else { + // Running from the file system URI uri; List componentList = new ArrayList<>(); componentList.add(firstPathComponent);