From d574d8d78b53269be8479bd47e18bbfe4627de5b Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Thu, 23 Apr 2020 11:13:03 +0800 Subject: [PATCH] fix: InvalidPathException when custom run directory is not valid --- .../jackhuang/hmcl/game/HMCLGameRepository.java | 14 ++++++++++---- .../org/jackhuang/hmcl/util/io/FileUtils.java | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java index 8f6c2c743..b6789d7d5 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java @@ -62,10 +62,16 @@ public class HMCLGameRepository extends DefaultGameRepository { @Override public File getRunDirectory(String id) { switch (getGameDirectoryType(id)) { - case VERSION_FOLDER: return getVersionRoot(id); - case ROOT_FOLDER: return super.getRunDirectory(id); - case CUSTOM: return new File(getVersionSetting(id).getGameDir()); - default: throw new Error(); + case VERSION_FOLDER: + return getVersionRoot(id); + case ROOT_FOLDER: + return super.getRunDirectory(id); + case CUSTOM: + File dir = new File(getVersionSetting(id).getGameDir()); + if (!FileUtils.isValidPath(dir)) return getVersionRoot(id); + return dir; + default: + throw new Error(); } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java index f58216f96..a76d3a1c0 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java @@ -390,4 +390,19 @@ public final class FileUtils { result.add(it); return result; } + + /** + * Tests whether the file is convertible to [java.nio.file.Path] or not. + * + * @param file the file to be tested + * @return true if the file is convertible to Path. + */ + public static boolean isValidPath(File file) { + try { + file.toPath(); + return true; + } catch (InvalidPathException ignored) { + return false; + } + } }