Skip to content

Commit

Permalink
Launcher: setupCommand now is awaited and must return a 0 exit code t…
Browse files Browse the repository at this point in the history
…o continue.
  • Loading branch information
luskaner committed Aug 15, 2024
1 parent 695e257 commit 719664e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
15 changes: 10 additions & 5 deletions launcher/internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,15 @@ var (
// Setup
fmt.Println("Setting up...")
if len(setupCommand) > 0 {
fmt.Printf("Running setup command '%s'...\n", viper.GetString("Config.SetupCommand"))
err = config.RunSetupCommand(setupCommand)
if err != nil {
fmt.Printf("Error: %s\n", err)
fmt.Printf("Running setup command '%s' and waiting for it to exit...\n", viper.GetString("Config.SetupCommand"))
result := config.RunSetupCommand(setupCommand)
if !result.Success() {
if result.Err != nil {
fmt.Printf("Error: %s\n", result.Err)
}
if result.ExitCode != common.ErrSuccess {
fmt.Printf(`Exit code: %d.`+"\n", result.ExitCode)
}
errorCode = internal.ErrSetupCommand
return
}
Expand Down Expand Up @@ -248,7 +253,7 @@ func Execute() error {
rootCmd.PersistentFlags().StringP("canBroadcastBattleServer", "b", "auto", `Whether or not to broadcast the game BattleServer to all interfaces in LAN (not just the most priority one)`)
rootCmd.PersistentFlags().BoolP("isolateMetadata", "m", true, "Isolate the metadata cache of the game, otherwise, it will be shared.")
rootCmd.PersistentFlags().BoolP("isolateProfiles", "p", false, "(Experimental) Isolate the users profile of the game, otherwise, it will be shared.")
rootCmd.PersistentFlags().String("setupCommand", "", `Executable to run (including arguments) to run first after the "Setting up..." line. You may use environment variables. Path names need to use double backslashes or be within single quotes.`)
rootCmd.PersistentFlags().String("setupCommand", "", `Executable to run (including arguments) to run first after the "Setting up..." line. # The command must return a 0 exit code to continue. If you need to keep it running spawn a new separate process. You may use environment variables. Path names need to use double backslashes or be within single quotes.`)
rootCmd.PersistentFlags().String("revertCommand", "", `Executable to run (including arguments) to run after setupCommand, game has exited and everything has been reverted. It may run before if there is an error. You may use environment variables. Path names need to use double backslashes or be within single quotes.`)
rootCmd.PersistentFlags().StringP("serverStart", "a", "auto", `Start the server if needed, "auto" will start a server if one is not already running, "true" (will start a server regardless if one is already running), "false" (will require an already running server).`)
rootCmd.PersistentFlags().StringP("serverStop", "o", "auto", `Stop the server if started, "auto" will stop the server if one was started, "false" (will not stop the server regardless if one was started), "true" (will not stop the server even if it was started).`)
Expand Down
18 changes: 14 additions & 4 deletions launcher/internal/cmdUtils/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
commonProcess "github.com/luskaner/aoe2DELanServer/common/process"
launcherExecutor "github.com/luskaner/aoe2DELanServer/launcher-common/executor"
"github.com/luskaner/aoe2DELanServer/launcher/internal/executor"
"golang.org/x/sys/windows"
)

type Config struct {
Expand Down Expand Up @@ -131,11 +132,20 @@ func GameRunning() bool {
return false
}

func (c *Config) RunSetupCommand(cmd []string) (err error) {
err = launcherExecutor.RunCommand(cmd)
if err == nil {
c.setupCommandRan = true
func (c *Config) RunSetupCommand(cmd []string) (result *launcherExecutor.ExecResult) {
var args []string
if len(cmd) > 1 {
args = cmd[1:]
}
result = launcherExecutor.ExecOptions{
File: cmd[0],
WindowState: windows.SW_NORMAL,
Wait: true,
SpecialFile: true,
Shell: true,
UseWorkingPath: true,
Args: args,
}.Exec()
return
}

Expand Down
1 change: 1 addition & 0 deletions launcher/resources/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ IsolateMetadata = true
# (Experimental) If true, the launcher will isolate the user profiles of the game, if false, it will be shared
IsolateProfiles = false
# Executable to run (including arguments) to run first after the "Setting up..." line.
# The command must return a 0 exit code to continue. If you need to keep it running spawn a new separate process.
# Path names need to use double backslashes or be within single quotes.
# Note: You may use environment variables.
SetupCommand =
Expand Down

0 comments on commit 719664e

Please sign in to comment.