Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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);
Comment on lines +268 to +269
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use selected export format when appending missing suffix

When the user saves a launch script without typing an extension, this branch always appends the OS default (bat/command/sh) and ignores which file type was selected in the save dialog. In practice, selecting the *.ps1 filter and saving launch will still produce a non-PowerShell script, silently changing script semantics (and on Windows can re-introduce cmd length limits). The fallback suffix should come from the chooser’s selected extension filter before falling back to OS defaults.

Useful? React with 👍 / 👎.

}

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.equals("bat") || ext.equals("ps1");
}
return ext.equals("sh") || ext.equals("bash") || ext.equals("command") || ext.equals("ps1");
}
Comment thread
WhatDamon marked this conversation as resolved.

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 @@ -646,8 +646,8 @@ public void makeLaunchScript(Path scriptFile) throws IOException {
if (!usePowerShell) {
if (isWindows && !scriptExtension.equals("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.equals("sh") || scriptExtension.equals("command") || scriptExtension.equals("bash")))
throw new IllegalArgumentException("The extension of " + scriptFile + " is not 'sh', 'bash', 'ps1' or 'command' in macOS/Linux");
Comment thread
WhatDamon marked this conversation as resolved.
Outdated
}

final Command commandLine = generateCommandLine(nativeFolder);
Expand Down