diff --git a/console/parser.go b/console/parser.go index 17bf2bf..fcc22e8 100644 --- a/console/parser.go +++ b/console/parser.go @@ -19,6 +19,7 @@ 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. @@ -26,6 +27,7 @@ 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) {}, } } @@ -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 } diff --git a/examples/install-rustserver/main.go b/examples/install-rustserver/main.go index 8d9423c..2122217 100644 --- a/examples/install-rustserver/main.go +++ b/examples/install-rustserver/main.go @@ -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) diff --git a/steamcmd.go b/steamcmd.go index 2763844..57985e1 100644 --- a/steamcmd.go +++ b/steamcmd.go @@ -13,8 +13,8 @@ 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, @@ -22,7 +22,7 @@ func New(stdout io.Writer, prompts []*Prompt) *SteamCMD { } //prepare command - cmd := "steamcmd" + cmd := path(steamPath) for _, prompt := range s.prompts { cmd += " +" + prompt.FullPrompt } @@ -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" +}