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