mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-03-11 08:22:14 +08:00
Restore ability to run without jar (#1875)
* Change JAR_FILE_SYSTEM to allow running from IDE without jar * Just grab /defaults/data * Restore FileUtils.getPathsFromResource
This commit is contained in:
parent
577772831c
commit
026ef9c51b
@ -2,7 +2,6 @@ package emu.grasscutter;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import emu.grasscutter.auth.AuthenticationSystem;
|
||||
import emu.grasscutter.auth.DefaultAuthentication;
|
||||
|
@ -5,17 +5,15 @@ import emu.grasscutter.Grasscutter;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class FileUtils {
|
||||
private static final FileSystem JAR_FILE_SYSTEM;
|
||||
private static final Path DATA_DEFAULT_PATH;
|
||||
private static final Path DATA_USER_PATH = Path.of(Grasscutter.config.folderStructure.data);
|
||||
private static final Path PACKETS_PATH = Path.of(Grasscutter.config.folderStructure.packets);
|
||||
@ -24,22 +22,28 @@ public final class FileUtils {
|
||||
private static final Path SCRIPTS_PATH;
|
||||
static {
|
||||
FileSystem fs = null;
|
||||
Path path = DATA_USER_PATH;
|
||||
// Setup Data paths
|
||||
// Get pathUri of the current running JAR
|
||||
Path path = null;
|
||||
// Setup access to jar resources
|
||||
try {
|
||||
URI jarUri = Grasscutter.class.getProtectionDomain()
|
||||
.getCodeSource()
|
||||
.getLocation()
|
||||
.toURI();
|
||||
fs = FileSystems.newFileSystem(Path.of(jarUri));
|
||||
path = fs.getPath("/defaults/data");
|
||||
var uri = Grasscutter.class.getResource("/defaults/data").toURI();
|
||||
switch (uri.getScheme()) {
|
||||
case "jar": // When running normally, as a jar
|
||||
case "zip": // Honestly I have no idea what setup would result in this, but this should work regardless
|
||||
fs = FileSystems.newFileSystem(uri, Map.of()); // Have to mount zip filesystem. This leaks, but we want to keep it forever anyway.
|
||||
// Fall-through
|
||||
case "file": // When running in an IDE
|
||||
path = Path.of(uri); // Can access directly
|
||||
break;
|
||||
default:
|
||||
Grasscutter.getLogger().error("Invalid URI scheme for class resources: "+uri.getScheme());
|
||||
break;
|
||||
}
|
||||
} catch (URISyntaxException | IOException e) {
|
||||
// Failed to load this jar. How?
|
||||
System.err.println("Failed to load jar?????????????????????");
|
||||
Grasscutter.getLogger().error("Failed to load jar?!");
|
||||
} finally {
|
||||
JAR_FILE_SYSTEM = fs;
|
||||
DATA_DEFAULT_PATH = path;
|
||||
Grasscutter.getLogger().debug("Setting path for default data: "+path.toAbsolutePath());
|
||||
}
|
||||
|
||||
// Setup Resources path
|
||||
@ -171,27 +175,22 @@ public final class FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
// From https://mkyong.com/java/java-read-a-file-from-resources-folder/
|
||||
public static List<Path> getPathsFromResource(String folder) throws URISyntaxException {
|
||||
List<Path> result = null;
|
||||
|
||||
try {
|
||||
// file walks JAR
|
||||
result = Files.walk(JAR_FILE_SYSTEM.getPath(folder))
|
||||
return Files.walk(Path.of(Grasscutter.class.getResource(folder).toURI()))
|
||||
.filter(Files::isRegularFile)
|
||||
.collect(Collectors.toList());
|
||||
} catch (Exception e) {
|
||||
} catch (IOException e) {
|
||||
// Eclipse puts resources in its bin folder
|
||||
File f = new File(System.getProperty("user.dir") + folder);
|
||||
|
||||
if (!f.exists() || f.listFiles().length == 0) {
|
||||
try {
|
||||
return Files.walk(Path.of(System.getProperty("user.dir"), folder))
|
||||
.filter(Files::isRegularFile)
|
||||
.collect(Collectors.toList());
|
||||
} catch (IOException ignored) {
|
||||
return null;
|
||||
}
|
||||
|
||||
result = Arrays.stream(f.listFiles()).map(File::toPath).toList();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
|
Loading…
x
Reference in New Issue
Block a user