Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ public static void generateLaunchScript(Profile profile, String id, Consumer<Lau
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(i18n("extension.ps1"), "*.ps1"));
Path file = FileUtils.toPath(chooser.showSaveDialog(Controllers.getStage()));
if (file != null) {
if (!isValidScriptExtension(FileUtils.getExtension(file))) {
String defaultExt = getDefaultScriptExtension();
file = file.resolveSibling(file.getFileName().toString() + "." + defaultExt);
}

LauncherHelper launcherHelper = new LauncherHelper(profile, account, id);
for (Consumer<LauncherHelper> injecter : injecters) {
injecter.accept(launcherHelper);
Expand All @@ -273,6 +278,21 @@ public static void generateLaunchScript(Profile profile, String id, Consumer<Lau
});
}

private static boolean isValidScriptExtension(String ext) {
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
return ext.equalsIgnoreCase("bat") || ext.equalsIgnoreCase("ps1");
}
return ext.equalsIgnoreCase("sh") || ext.equalsIgnoreCase("bash") || ext.equalsIgnoreCase("command") || ext.equalsIgnoreCase("ps1");
}

private static String getDefaultScriptExtension() {
return switch (OperatingSystem.CURRENT_OS) {
case WINDOWS -> "bat";
case MACOS -> "command";
default -> "sh";
};
}

@SafeVarargs
public static void launch(Profile profile, String id, Consumer<LauncherHelper>... injecters) {
if (!checkVersionForLaunching(profile, id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,10 @@ public void makeLaunchScript(Path scriptFile) throws IOException {
boolean usePowerShell = "ps1".equals(scriptExtension);

if (!usePowerShell) {
if (isWindows && !scriptExtension.equals("bat"))
if (isWindows && !scriptExtension.equalsIgnoreCase("bat"))
throw new IllegalArgumentException("The extension of " + scriptFile + " is not 'bat' or 'ps1' in Windows");
else if (!isWindows && !(scriptExtension.equals("sh") || scriptExtension.equals("command")))
throw new IllegalArgumentException("The extension of " + scriptFile + " is not 'sh', 'ps1' or 'command' in macOS/Linux");
else if (!isWindows && !(scriptExtension.equalsIgnoreCase("sh") || scriptExtension.equalsIgnoreCase("command") || scriptExtension.equalsIgnoreCase("bash")))
throw new IllegalArgumentException("The extension of " + scriptFile + " is not 'sh', 'bash', 'ps1' or 'command' in macOS/Linux");
}

final Command commandLine = generateCommandLine(nativeFolder);
Expand Down