Skip to content

Commit

Permalink
Merge pull request #2 from JensvandeWiel/1-add-appinstalled-hook
Browse files Browse the repository at this point in the history
1 add appinstalled hook and cutom steam path
  • Loading branch information
JensvandeWiel authored Aug 7, 2023
2 parents 5efe984 + 342036a commit 9de8d63
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
23 changes: 23 additions & 0 deletions console/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ const (
// Parser is a parser for the steamcmd console output. Right now it only parses the progress of downloading, verifying and preallocating. To capture other output use the stdout.
type Parser struct {
OnInformationReceived func(action Action, progress float64, currentWritten, total uint64)
OnAppInstalled func(app uint32)
}

// NewParser creates a new Parser instance. this must be used, if not the parser will not work.
func NewParser() *Parser {
// Set empty function because otherwise it will panic, because not every user will use this.
return &Parser{
OnInformationReceived: func(action Action, progress float64, currentWritten, total uint64) {},
OnAppInstalled: func(app uint32) {},
}
}

Expand All @@ -49,6 +51,27 @@ func (p *Parser) Write(data []byte) (int, error) {
return 0, err
}
go p.OnInformationReceived(Preallocating, progress, current, total)
case strings.Contains(string(data), "fully installed"):
// Define the regular expression pattern to match the App ID
pattern := `App '(\d+)'`

// Compile the regular expression
regex := regexp.MustCompile(pattern)

// Find the first match of the pattern in the input string
match := regex.FindStringSubmatch(string(data))

// Extract the App ID from the matched groups
if len(match) >= 2 {
appIDStr := match[1]

// Parse the App ID string to uint32
appID, err := strconv.ParseUint(appIDStr, 10, 32)
if err != nil {
return 0, err
}
p.OnAppInstalled(uint32(appID))
}
}
return len(data), nil
}
Expand Down
7 changes: 5 additions & 2 deletions examples/install-rustserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ func main() {
gosteamcmd.AppUpdate(258550, "", true),
}

cmd := gosteamcmd.New(os.Stdout, prompts)
cmd := gosteamcmd.New(os.Stdout, prompts, "")

cmd.Console.Parser.OnInformationReceived = func(action console.Action, progress float64, currentWritten, total uint64) {
println("")
}
cmd.Console.Parser.OnAppInstalled = func(app uint32) {
println("App installed: ", app, " Yay!")
}

err := cmd.Run()
_, err := cmd.Run()

if err != nil {
panic(err)
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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
)
require github.com/UserExistsError/conpty v0.1.1

require golang.org/x/sys v0.11.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ github.com/UserExistsError/conpty v0.1.1 h1:cHDsU/XeoeDAQmVvCTV53SrXLG39YJ4++Pp3
github.com/UserExistsError/conpty v0.1.1/go.mod h1:PDglKIkX3O/2xVk0MV9a6bCWxRmPVfxqZoTG/5sSd9I=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13 changes: 10 additions & 3 deletions steamcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ type SteamCMD struct {
Stdout io.Writer
}

// New creates a new SteamCMD instance.
func New(stdout io.Writer, prompts []*Prompt) *SteamCMD {
// New creates a new SteamCMD instance. steamPath can be left empty like "" to use global path If set it uses the custom path
func New(stdout io.Writer, prompts []*Prompt, steamPath string) *SteamCMD {

s := &SteamCMD{
prompts: prompts,
Stdout: stdout,
}

//prepare command
cmd := "steamcmd"
cmd := path(steamPath)
for _, prompt := range s.prompts {
cmd += " +" + prompt.FullPrompt
}
Expand All @@ -36,3 +36,10 @@ func New(stdout io.Writer, prompts []*Prompt) *SteamCMD {
func (s *SteamCMD) Run() (uint32, error) {
return s.Console.Run()
}

func path(steamPath string) string {
if steamPath != "" {
return steamPath
}
return "steamcmd"
}

0 comments on commit 9de8d63

Please sign in to comment.