Skip to content

Commit

Permalink
Added OnAppinstalled and custom path for steamcmd
Browse files Browse the repository at this point in the history
  • Loading branch information
JensvandeWiel committed Aug 7, 2023
1 parent 6318793 commit 342036a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 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
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 342036a

Please sign in to comment.