From 512021b23477f71bcae0737183d7d3a3d222928f Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Thu, 19 May 2022 00:10:02 -0700 Subject: [PATCH] Fix dataloader with eclipse --- .../java/emu/grasscutter/data/DataLoader.java | 9 ++++--- .../java/emu/grasscutter/utils/FileUtils.java | 27 ++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/emu/grasscutter/data/DataLoader.java b/src/main/java/emu/grasscutter/data/DataLoader.java index dc7e67281..30b6f711b 100644 --- a/src/main/java/emu/grasscutter/data/DataLoader.java +++ b/src/main/java/emu/grasscutter/data/DataLoader.java @@ -7,8 +7,10 @@ import emu.grasscutter.utils.FileUtils; import emu.grasscutter.utils.Utils; import java.io.*; +import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.List; +import java.util.regex.Pattern; import static emu.grasscutter.Configuration.DATA; @@ -46,17 +48,18 @@ public class DataLoader { } public static void CheckAllFiles() { - + String pathSplitter = "defaults" + Pattern.quote(FileSystems.getDefault().getSeparator()) + "data" + Pattern.quote(FileSystems.getDefault().getSeparator()); + try { List filenames = FileUtils.getPathsFromResource("/defaults/data/"); for (Path file : filenames) { - String relativePath = String.valueOf(file).split("/defaults/data/")[1]; + String relativePath = String.valueOf(file).split(pathSplitter)[1]; CheckAndCopyData(relativePath); } } catch (Exception e) { - Grasscutter.getLogger().error("An error occurred while trying to check the data folder. \n" + e); + Grasscutter.getLogger().error("An error occurred while trying to check the data folder. \n", e); } GenerateGachaMappings(); diff --git a/src/main/java/emu/grasscutter/utils/FileUtils.java b/src/main/java/emu/grasscutter/utils/FileUtils.java index 17e680576..5d42dd88f 100644 --- a/src/main/java/emu/grasscutter/utils/FileUtils.java +++ b/src/main/java/emu/grasscutter/utils/FileUtils.java @@ -9,6 +9,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.*; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -76,23 +77,29 @@ public final class FileUtils { // From https://mkyong.com/java/java-read-a-file-from-resources-folder/ public static List getPathsFromResource(String folder) throws URISyntaxException, IOException { - List result; - - // get path of the current running JAR + List result = null; + + // Get path of the current running JAR String jarPath = Grasscutter.class.getProtectionDomain() .getCodeSource() .getLocation() .toURI() .getPath(); - // file walks JAR - URI uri = URI.create("jar:file:" + jarPath); - try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) { - result = Files.walk(fs.getPath(folder)) - .filter(Files::isRegularFile) - .collect(Collectors.toList()); + try { + // file walks JAR + URI uri = URI.create("jar:file:" + jarPath); + try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) { + result = Files.walk(fs.getPath(folder)) + .filter(Files::isRegularFile) + .collect(Collectors.toList()); + } + } catch (Exception e) { + // Eclipse puts resources in its bin folder + File f = new File(jarPath + "defaults/data/"); + result = Arrays.stream(f.listFiles()).map(File::toPath).toList(); } - + return result; }