aboutsummaryrefslogtreecommitdiff
path: root/steam/steam.go
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2023-01-20 00:35:09 -0500
committerMitchell Riedstra <mitch@riedstra.dev>2023-01-20 00:35:09 -0500
commitf07efbb6fc7a63055a8424799ce03a5f37539873 (patch)
treeff5983b2cae4cc9b8f2f346a47cb3eb23b2f79ae /steam/steam.go
parentcbfd82db8a20be32ffa82a1afa860729f3097de6 (diff)
downloadsteam-export-dev-wip.tar.gz
steam-export-dev-wip.tar.xz
Diffstat (limited to 'steam/steam.go')
-rw-r--r--steam/steam.go53
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
}