aboutsummaryrefslogtreecommitdiff
path: root/steam/game.go
blob: 8c387d28d0753d7391a2680e5c13b76c137470c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package steam

import (
	"os"
	"path/filepath"
)

// Game represents an actual game in the steam Library. The purpose is only
// to provide info on a game.
type Game struct {
	Name        string `json:"Name" example:"Doom"`
	LibraryPath string `json:"LibraryPath" example:"C:\\Program Files (x86)\\Steam\\steamapps"`
	Size        int64  `json:"Size" example:"12345"`
}

// GetSizeBytes returns the size in bytes, calling SetSizeInfo info Size is
// currently == 0
func (g *Game) GetSizeBytes() int64 {
	if g.Size == 0 {
		_ = g.SetSizeInfo()
	}
	return g.Size
}

// GetSize returns the size of a game in a pretty format. If size is 0
// it will call SetSizeInfo before returning
func (g *Game) GetSize() string {
	if g.Size == 0 {
		_ = g.SetSizeInfo()
	}
	return formatBytes(g.Size)
}

// SetSizeInfo reads the size information for the game off of the disk
// and stores it in the Size struct element
func (g *Game) SetSizeInfo() error {
	pth := filepath.Join(g.LibraryPath, "common", g.Name)
	return filepath.Walk(pth, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if info.Mode().IsRegular() {
			g.Size += info.Size()
		}

		return nil
	})
}