Skip to content

Commit

Permalink
Launcher: Fixed parsing of Server.ExecutableArgs and Client.Executabl…
Browse files Browse the repository at this point in the history
…eArgs.
  • Loading branch information
luskaner committed Aug 13, 2024
1 parent af90738 commit f4dec41
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions launcher/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
golang.org/x/sys v0.23.0
mvdan.cc/sh/v3 v3.8.0
)

require (
Expand Down
3 changes: 3 additions & 0 deletions launcher/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
Expand Down Expand Up @@ -85,3 +86,5 @@ gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mvdan.cc/sh/v3 v3.8.0 h1:ZxuJipLZwr/HLbASonmXtcvvC9HXY9d2lXZHnKGjFc8=
mvdan.cc/sh/v3 v3.8.0/go.mod h1:w04623xkgBVo7/IUK89E0g8hBykgEpN0vgOj3RJr6MY=
27 changes: 23 additions & 4 deletions launcher/internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/sys/windows"
"mvdan.cc/sh/v3/shell"
"os"
"os/signal"
"path/filepath"
Expand All @@ -28,6 +29,11 @@ const falseValue = "false"
var configPaths = []string{"resources", "."}
var config = &cmdUtils.Config{}

func parseCommandArgs(name string) (args []string, err error) {
args, err = shell.Fields(viper.GetString(name), nil)
return
}

var (
Version string
cfgFile string
Expand Down Expand Up @@ -74,6 +80,19 @@ var (
errorCode = internal.ErrInvalidServerStop
return
}
serverArgs, err := parseCommandArgs("Server.ExecutableArgs")
if err != nil {
fmt.Println("Failed to parse server executable arguments")
errorCode = internal.ErrInvalidServerArgs
return
}
var clientArgs []string
clientArgs, err = parseCommandArgs("Client.ExecutableArgs")
if err != nil {
fmt.Println("Failed to parse server executable arguments")
errorCode = internal.ErrInvalidClientArgs
return
}
canAddHost := viper.GetBool("Config.CanAddHost")
isolateMetadata := viper.GetBool("Config.IsolateMetadata")
isolateProfiles := viper.GetBool("Config.IsolateProfiles")
Expand Down Expand Up @@ -170,7 +189,7 @@ var (
return
}
} else {
errorCode, serverHost = config.StartServer(serverExecutable, serverStop == "true", canTrustCertificate != "false")
errorCode, serverHost = config.StartServer(serverExecutable, serverArgs, serverStop == "true", canTrustCertificate != "false")
if errorCode != common.ErrSuccess {
return
}
Expand All @@ -190,7 +209,7 @@ var (
errorMayBeConfig = true
return
}
errorCode = config.LaunchAgentAndGame(clientExecutable, canTrustCertificate, canBroadcastBattleServer)
errorCode = config.LaunchAgentAndGame(clientExecutable, clientArgs, canTrustCertificate, canBroadcastBattleServer)
},
}
)
Expand All @@ -210,9 +229,9 @@ func Execute() error {
rootCmd.PersistentFlags().StringP("server", "s", "", `Hostname of the server to connect to. If not absent, serverStart will be assumed to be false. Ignored otherwise`)
serverExe := common.GetExeFileName(false, common.Server)
rootCmd.PersistentFlags().StringP("serverPath", "e", "auto", fmt.Sprintf(`The executable path of the server, "auto", will be try to execute in this order ".\%s", ".\%s\%s", "..\%s" and finally "..\%s\%s", otherwise set the path (relative or absolute).`, serverExe, common.Server, serverExe, serverExe, common.Server, serverExe))
rootCmd.PersistentFlags().StringArrayP("serverPathArgs", "r", []string{}, `The arguments to pass to the server executable if starting it. Can be set multiple times. See the server for available arguments.`)
rootCmd.PersistentFlags().StringP("serverPathArgs", "r", "[]string{}", `The arguments to pass to the server executable if starting it. Execute the server help flag for available arguments. Path names need to use double backslashes or be within single quotes.`)
rootCmd.PersistentFlags().StringP("clientExe", "l", "auto", `The type of game client or the path. "auto" will use the Steam and then the Microsoft Store one if found. Use a path to the game launcher, "steam" or "msstore" to use the default launcher.`)
rootCmd.PersistentFlags().StringArrayP("clientExeArgs", "i", []string{}, `The arguments to pass to the client launcher if it is custom. Can be set multiple times.`)
rootCmd.PersistentFlags().StringP("clientExeArgs", "i", "", `The arguments to pass to the client launcher if it is custom. Path names need to use double backslashes or be within single quotes.`)
if err := viper.BindPFlag("Config.CanAddHost", rootCmd.PersistentFlags().Lookup("canAddHost")); err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions launcher/internal/cmdUtils/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/luskaner/aoe2DELanServer/launcher/internal"
"github.com/luskaner/aoe2DELanServer/launcher/internal/executor"
"github.com/luskaner/aoe2DELanServer/launcher/internal/game"
"github.com/spf13/viper"
"golang.org/x/sys/windows"
)

Expand All @@ -21,7 +20,7 @@ func (c *Config) KillAgent() {
}
}

func (c *Config) LaunchAgentAndGame(executable string, canTrustCertificate string, canBroadcastBattleServer string) (errorCode int) {
func (c *Config) LaunchAgentAndGame(executable string, args []string, canTrustCertificate string, canBroadcastBattleServer string) (errorCode int) {
fmt.Println("Looking for the game...")
executer := game.MakeExecutor(executable)
var customExecutor game.CustomExecutor
Expand Down Expand Up @@ -70,14 +69,13 @@ func (c *Config) LaunchAgentAndGame(executable string, canTrustCertificate strin
fmt.Println("Starting game...")
}
var result *commonExecutor.ExecResult
executableArgs := viper.GetStringSlice("Client.ExecutableArgs")

if result = executer.Execute(executableArgs); !result.Success() && result.Err != nil {
if result = executer.Execute(args); !result.Success() && result.Err != nil {
if customExecutor.Executable != "" && errors.Is(result.Err, windows.ERROR_ELEVATION_REQUIRED) {
if canTrustCertificate == "user" {
fmt.Println("Using a user certificate. If it fails to connect to the server, try setting the config/option setting 'CanTrustCertificate' to 'local'.")
}
result = customExecutor.ExecuteElevated(executableArgs)
result = customExecutor.ExecuteElevated(args)
}
}
if !result.Success() {
Expand Down
6 changes: 2 additions & 4 deletions launcher/internal/cmdUtils/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
commonExecutor "github.com/luskaner/aoe2DELanServer/launcher-common/executor"
"github.com/luskaner/aoe2DELanServer/launcher/internal"
"github.com/luskaner/aoe2DELanServer/launcher/internal/server"
"github.com/spf13/viper"
"net"
"sort"
"strings"
Expand Down Expand Up @@ -159,7 +158,7 @@ func ListenToServerAnnouncementsAndSelect(ports []int) (errorCode int, host stri
return
}

func (c *Config) StartServer(executable string, stop bool, canTrustCertificate bool) (errorCode int, ip string) {
func (c *Config) StartServer(executable string, args []string, stop bool, canTrustCertificate bool) (errorCode int, ip string) {
serverExecutablePath := server.GetExecutablePath(executable)
if serverExecutablePath == "" {
fmt.Println("Cannot find server executable path. Set it manually in Server.Executable.")
Expand Down Expand Up @@ -202,8 +201,7 @@ func (c *Config) StartServer(executable string, stop bool, canTrustCertificate b
}
var result *commonExecutor.ExecResult
var serverExe string
result, serverExe, ip = server.StartServer(stopStr, executable,
viper.GetStringSlice("Server.ExecutableArgs"))
result, serverExe, ip = server.StartServer(stopStr, executable, args)
if result.Success() {
fmt.Println("Server started.")
c.SetServerExe(serverExe)
Expand Down
2 changes: 2 additions & 0 deletions launcher/internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ const (
ErrTrustCert
ErrMetadataProfilesSetup
ErrAgentStart
ErrInvalidServerArgs
ErrInvalidClientArgs
)
4 changes: 3 additions & 1 deletion launcher/resources/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Start = auto
# 3. "..\server.exe"
# 4. "..\server\server.exe"
Executable = auto
# The arguments to pass to the server executable if starting it. See the server for available arguments
# The arguments to pass to the server executable if starting it. Execute the server help flag for available arguments.
# Note: Path names need to use double backslashes or be within single quotes.
ExecutableArgs =
# The host of the server to connect to if Start = false, if Start = true/auto this will be ignored
# The host may be a DNS name or IP (IPv4 or IPv6) but IPv4 is recommended. 0.0.0.0 means every local interface IP.
Expand All @@ -40,4 +41,5 @@ AnnouncePorts = 31978
# Use a path to the game launcher, 'steam' or 'msstore' to use the default launcher
Executable = auto
# The arguments to pass to the client launcher if it is custom
# Note: Path names need to use double backslashes or be within single quotes.
ExecutableArgs =

0 comments on commit f4dec41

Please sign in to comment.