diff options
Diffstat (limited to 'steam/steam.go')
| -rw-r--r-- | steam/steam.go | 53 |
1 files changed, 25 insertions, 28 deletions
diff --git a/steam/steam.go b/steam/steam.go index 03fa51e..418cbf4 100644 --- a/steam/steam.go +++ b/steam/steam.go @@ -6,15 +6,21 @@ package steam import ( "errors" "fmt" - "io/ioutil" + "os" "regexp" + "riedstra.dev/mitch/steam-export/tasks" "sync" + + "github.com/barkimedes/go-deepcopy" ) var ( - E_GameDoesNotExist = errors.New("Game does not exist") - E_BadURI = errors.New("The URI supplied is not understood") - E_OperationConflict = errors.New("Another conflicting job is running on this game right now") + E_GameDoesNotExist = errors.New("game does not exist") + E_BadURI = errors.New("the URI supplied is not understood") + E_OperationConflict = errors.New("another conflicting job is running on this game right now") + E_NoEstimatedSize = errors.New("expected an estimated size, got nil") + E_LibraryLocked = errors.New("cannot process library with actions running, library is locked") + E_LibraryNoCommon = errors.New("no common directory") ) // Library is used to represent the steam library, the Games map is populated @@ -26,19 +32,11 @@ var ( type Library struct { folder string games map[string]*Game - status *Jobs + status *tasks.Group m sync.Mutex } -// 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"` -} - var slugregexp = regexp.MustCompile(`[^-0-9A-Za-z_:.]`) // Slug returns a safer version of the name with spaces and other chars @@ -51,10 +49,7 @@ func (g Game) Slug() string { // if any func NewLibrary(path string) (*Library, error) { l := &Library{ - status: &Jobs{ - running: make([]*Job, 0), - previous: make([]*Job, 0), - }, + status: tasks.NewGroup(), } err := l.ProcessLibrary(path) if err != nil { @@ -84,16 +79,18 @@ func (l *Library) Folder() string { func (l *Library) Games() map[string]*Game { l.m.Lock() defer l.m.Unlock() - return l.games + g := deepcopy.MustAnything(l.games) + return g.(map[string]*Game) } -// Jobs returns the current *Jobs struct which can be used to keep track -// of any long running operations on the library as well as any errors +// Status returns the current Jobs struct which can be used to keep track +// of any long-running operations on the library as well as any errors // encountered along the way -func (l *Library) Status() Jobs { +func (l *Library) Status() *tasks.Group { l.m.Lock() defer l.m.Unlock() - return *l.status + s2 := deepcopy.MustAnything(l.status) + return s2.(*tasks.Group) } // Refresh simply calls ProcessLibrary to refresh the entire contents of the @@ -105,19 +102,19 @@ func (l *Library) Refresh() error { // ProcessLibrary Populates the "Folder" and "Games" fields based on the // provided directory. Returns an error if any jobs are currently running func (s *Library) ProcessLibrary(r string) error { - if s.status.Running() { - return errors.New("Cannot process library with actions running") + if len(s.status.Running()) > 0 { + return E_LibraryLocked } if !hasCommon(r) { - return fmt.Errorf("No common directory in: %s", r) + return fmt.Errorf("in: '%s': %w", r, E_LibraryNoCommon) } s.m.Lock() defer s.m.Unlock() s.games = make(map[string]*Game) - dirs, err := ioutil.ReadDir(r + "/common") + dirs, err := os.ReadDir(r + "/common") if err != nil { return err } @@ -128,7 +125,7 @@ func (s *Library) ProcessLibrary(r string) error { Name: f.Name(), LibraryPath: r, } - g.SetSizeInfo() + _ = g.SetSizeInfo() s.games[f.Name()] = g } @@ -147,7 +144,7 @@ func (s *Library) String() (str string) { } func hasCommon(d string) bool { - dirs, err := ioutil.ReadDir(d) + dirs, err := os.ReadDir(d) if err != nil { return false } |
