From a6192659760693f5d221bb75455ede2e7acdb7b0 Mon Sep 17 00:00:00 2001 From: Jens van de Wiel Date: Thu, 3 Aug 2023 21:26:34 +0200 Subject: [PATCH] added everything to make it have basic functionality. --- console/console_windows.go | 41 +++++++++++++++++++++++++++++ examples/1/main.go | 5 ---- examples/install-rustserver/main.go | 17 ++++++++++++ go.mod | 5 ++++ propmt.go => prompt.go | 16 ++++++++--- readme.md | 6 +++++ steamcmd.go | 30 +++++++++++++++++++-- 7 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 console/console_windows.go delete mode 100644 examples/1/main.go create mode 100644 examples/install-rustserver/main.go rename propmt.go => prompt.go (66%) create mode 100644 readme.md diff --git a/console/console_windows.go b/console/console_windows.go new file mode 100644 index 0000000..da765e0 --- /dev/null +++ b/console/console_windows.go @@ -0,0 +1,41 @@ +//go:build windows + +package console + +import ( + "context" + "github.com/UserExistsError/conpty" + "io" +) + +type Console struct { + stdout io.Writer + commandLine string + conPTY *conpty.ConPty + ExitCode uint32 +} + +func New(exePath string, stdout io.Writer) *Console { + return &Console{ + commandLine: exePath, + stdout: stdout, + } +} + +func (c *Console) Run() error { + var err error + c.conPTY, err = conpty.Start(c.commandLine) + if err != nil { + return err + } + defer c.conPTY.Close() + + go io.Copy(c.stdout, c.conPTY) + + c.ExitCode, err = c.conPTY.Wait(context.Background()) + if err != nil { + return err + } + + return nil +} diff --git a/examples/1/main.go b/examples/1/main.go deleted file mode 100644 index 7905807..0000000 --- a/examples/1/main.go +++ /dev/null @@ -1,5 +0,0 @@ -package main - -func main() { - -} diff --git a/examples/install-rustserver/main.go b/examples/install-rustserver/main.go new file mode 100644 index 0000000..dbc77d5 --- /dev/null +++ b/examples/install-rustserver/main.go @@ -0,0 +1,17 @@ +package main + +import "github.com/jensvandewiel/gosteamcmd" + +func main() { + //this code follows the steps of: https://www.rustafied.com/how-to-host-your-own-rust-server + cmd := gosteamcmd.New() + cmd.Prompts = append(cmd.Prompts, gosteamcmd.ForceInstallDir("c:\\rustserver\\")) + cmd.Prompts = append(cmd.Prompts, gosteamcmd.Login("", "")) + cmd.Prompts = append(cmd.Prompts, gosteamcmd.AppUpdate(258550, "", false)) + //running it headless means it will not output anything + err := cmd.RunHeadless() + + if err != nil { + panic(err) + } +} diff --git a/go.mod b/go.mod index 2c43f2a..b9a6846 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module github.com/jensvandewiel/gosteamcmd go 1.20 + +require ( + github.com/UserExistsError/conpty v0.1.1 // indirect + golang.org/x/sys v0.8.0 // indirect +) diff --git a/propmt.go b/prompt.go similarity index 66% rename from propmt.go rename to prompt.go index 4faf392..e760fc3 100644 --- a/propmt.go +++ b/prompt.go @@ -7,6 +7,7 @@ type PromptType int const ( AppUpdatePrompt PromptType = iota LoginPrompt + ForceInstallDirPrompt ) type Prompt struct { @@ -15,7 +16,7 @@ type Prompt struct { } // AppUpdate updates the given appID. If beta is not empty, it will update to the given beta. If validate is true, it will validate the files. -func AppUpdate(appID int, beta string, validate bool) Prompt { +func AppUpdate(appID int, beta string, validate bool) *Prompt { cmd := "app_update " cmd += strconv.Itoa(appID) + " " if beta != "" { @@ -24,16 +25,23 @@ func AppUpdate(appID int, beta string, validate bool) Prompt { if validate { cmd += "validate" } - return Prompt{cmd, AppUpdatePrompt} + return &Prompt{cmd, AppUpdatePrompt} } // Login logs into SteamCMD with the given username and password. If the arguments are empty, it will login as anonymous. -func Login(username string, password string) Prompt { +func Login(username string, password string) *Prompt { cmd := "login " if username != "" { cmd += username + " " + password } else { cmd += "anonymous" } - return Prompt{cmd, LoginPrompt} + return &Prompt{cmd, LoginPrompt} +} + +func ForceInstallDir(directory string) *Prompt { + cmd := "force_install_dir " + cmd += directory + + return &Prompt{cmd, ForceInstallDirPrompt} } diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..6c30864 --- /dev/null +++ b/readme.md @@ -0,0 +1,6 @@ +# This is a W.I.P. project, so it's not finished yet. + +To use you must have steamcmd instaled and added it to your path, for now you can only use the library from windows + +## Contribute +I would love to see people contributing to this project, so if you want to contribute just fork the project and make a pull request. \ No newline at end of file diff --git a/steamcmd.go b/steamcmd.go index 8ab3557..a6714d2 100644 --- a/steamcmd.go +++ b/steamcmd.go @@ -1,9 +1,35 @@ package gosteamcmd +import ( + "github.com/jensvandewiel/gosteamcmd/console" + "os" +) + type SteamCMD struct { - prompts *[]Prompt + // Prompts contains all the commands that will be executed. + Prompts []*Prompt + console *console.Console } func New() *SteamCMD { - return &SteamCMD{} + return &SteamCMD{ + Prompts: make([]*Prompt, 0), + } +} + +func (s *SteamCMD) RunHeadless() error { + cmd := "steamcmd" + + for _, prompt := range s.Prompts { + cmd += " +" + prompt.FullPrompt + } + + cmd += " +quit" + + s.console = console.New(cmd, os.Stdout) + err := s.console.Run() + if err != nil { + return err + } + return nil }