From dff893ba4bbc622c8e661a725cf38e924409ff59 Mon Sep 17 00:00:00 2001 From: Glavo Date: Sat, 2 Mar 2024 00:01:02 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20#2870:=20=E5=AF=BC=E5=87=BA=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E6=97=B6=E9=A2=84=E8=A7=A3=E6=9E=90=20PreLaunchComman?= =?UTF-8?q?d=20=E4=B8=8E=20PostExitCommand=20(#2871)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix #2870: 导出脚本时预解析 PreLaunchCommand 与 PostExitCommand * fix checkstyle --- .../hmcl/launch/DefaultLauncher.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java index 6653e51f8..4bf3bf2d3 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java @@ -560,6 +560,7 @@ public class DefaultLauncher extends Launcher { final Command commandLine = generateCommandLine(nativeFolder); final String command = usePowerShell ? null : commandLine.commandLine.toString(); + Map envVars = getEnvVars(); if (!usePowerShell && isWindows) { if (command.length() > 8192) { // maximum length of the command in cmd @@ -596,7 +597,7 @@ public class DefaultLauncher extends Launcher { writer.write(CommandBuilder.pwshString(options.getGameDir().getAbsoluteFile().getParent())); writer.newLine(); } - for (Map.Entry entry : getEnvVars().entrySet()) { + for (Map.Entry entry : envVars.entrySet()) { writer.write("$Env:" + entry.getKey() + "="); writer.write(CommandBuilder.pwshString(entry.getValue())); writer.newLine(); @@ -605,19 +606,38 @@ public class DefaultLauncher extends Launcher { writer.write(CommandBuilder.pwshString(repository.getRunDirectory(version.getId()).getAbsolutePath())); writer.newLine(); + + if (StringUtils.isNotBlank(options.getPreLaunchCommand())) { + writer.write('&'); + for (String rawCommand : StringUtils.tokenize(options.getPreLaunchCommand(), envVars)) { + writer.write(' '); + writer.write(CommandBuilder.pwshString(rawCommand)); + } + writer.newLine(); + } + writer.write('&'); for (String rawCommand : commandLine.commandLine.asList()) { writer.write(' '); writer.write(CommandBuilder.pwshString(rawCommand)); } writer.newLine(); + + if (StringUtils.isNotBlank(options.getPostExitCommand())) { + writer.write('&'); + for (String rawCommand : StringUtils.tokenize(options.getPostExitCommand(), envVars)) { + writer.write(' '); + writer.write(CommandBuilder.pwshString(rawCommand)); + } + writer.newLine(); + } } else { if (isWindows) { writer.write("@echo off"); writer.newLine(); writer.write("set APPDATA=" + options.getGameDir().getAbsoluteFile().getParent()); writer.newLine(); - for (Map.Entry entry : getEnvVars().entrySet()) { + for (Map.Entry entry : envVars.entrySet()) { writer.write("set " + entry.getKey() + "=" + CommandBuilder.toBatchStringLiteral(entry.getValue())); writer.newLine(); } @@ -626,7 +646,7 @@ public class DefaultLauncher extends Launcher { } else { writer.write("#!/usr/bin/env bash"); writer.newLine(); - for (Map.Entry entry : getEnvVars().entrySet()) { + for (Map.Entry entry : envVars.entrySet()) { writer.write("export " + entry.getKey() + "=" + CommandBuilder.toShellStringLiteral(entry.getValue())); writer.newLine(); } @@ -638,13 +658,14 @@ public class DefaultLauncher extends Launcher { } writer.newLine(); if (StringUtils.isNotBlank(options.getPreLaunchCommand())) { - writer.write(options.getPreLaunchCommand()); + writer.write(new CommandBuilder().addAll(StringUtils.tokenize(options.getPreLaunchCommand(), envVars)).toString()); writer.newLine(); } writer.write(command); writer.newLine(); + if (StringUtils.isNotBlank(options.getPostExitCommand())) { - writer.write(options.getPostExitCommand()); + writer.write(new CommandBuilder().addAll(StringUtils.tokenize(options.getPostExitCommand(), envVars)).toString()); writer.newLine(); }