fix: InvalidPathException when custom run directory is not valid
This commit is contained in:
@@ -62,10 +62,16 @@ public class HMCLGameRepository extends DefaultGameRepository {
|
|||||||
@Override
|
@Override
|
||||||
public File getRunDirectory(String id) {
|
public File getRunDirectory(String id) {
|
||||||
switch (getGameDirectoryType(id)) {
|
switch (getGameDirectoryType(id)) {
|
||||||
case VERSION_FOLDER: return getVersionRoot(id);
|
case VERSION_FOLDER:
|
||||||
case ROOT_FOLDER: return super.getRunDirectory(id);
|
return getVersionRoot(id);
|
||||||
case CUSTOM: return new File(getVersionSetting(id).getGameDir());
|
case ROOT_FOLDER:
|
||||||
default: throw new Error();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -390,4 +390,19 @@ public final class FileUtils {
|
|||||||
result.add(it);
|
result.add(it);
|
||||||
return result;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user