diff options
Diffstat (limited to 'steam')
| -rw-r--r-- | steam/archive.go | 55 | ||||
| -rw-r--r-- | steam/package.go | 112 | ||||
| -rw-r--r-- | steam/steam.go | 62 | ||||
| -rw-r--r-- | steam/unix.go | 10 | ||||
| -rw-r--r-- | steam/windows.go | 5 |
5 files changed, 144 insertions, 100 deletions
diff --git a/steam/archive.go b/steam/archive.go new file mode 100644 index 0000000..d997051 --- /dev/null +++ b/steam/archive.go @@ -0,0 +1,55 @@ +package steam + +import ( + "archive/tar" + "io" + "os" + "path/filepath" + "strings" +) + +func TarWalkfn(writer *tar.Writer) filepath.WalkFunc { + return func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if info.IsDir() { + return nil + } + + f, err := os.Open(path) + if err != nil { + return err + } + defer f.Close() + + // Convert Windows paths to Unix paths + path = strings.Replace(path, "\\", "/", -1) + + // TODO; See if tar.FileInfoheader() could be used instead + // without the pathing issues I encountered + h := &tar.Header{ + Name: path, + Size: info.Size(), + // I don't like it... but it helps with platform compatibility + Mode: 0664, + ModTime: info.ModTime(), + } + + err = writer.WriteHeader(h) + if err != nil { + return err + } + + _, err = io.Copy(writer, f) + if err != nil { + // TODO: Figure out how to add more useful information to + // These errors + // fmt.Fprintln(os.Stderr, f.Name()) + return err + } + + return nil + } +} diff --git a/steam/package.go b/steam/package.go index a73a6bc..9000cba 100644 --- a/steam/package.go +++ b/steam/package.go @@ -1,94 +1,96 @@ package steam import ( - // "fmt" - "riedstra.dev/mitch/steam-export/archive" + "archive/tar" + "io" "os" "path/filepath" + "strings" ) -func (l *Library) PackageGameToFile(index int, file, compress string) error { - g := l.Games[index] - - working_dir, err := os.Getwd() - if err != nil { +// Package writes the package, returning bytes written and an error if any +func (g *Game) Package(wr io.Writer) error { + if err := os.Chdir(g.LibraryPath); err != nil { return err } - // output := working_dir + "/" + g + ".tar" - output, err := filepath.Abs(file) + acf, err := FindACF(g.LibraryPath, g.Name) if err != nil { return err } - os.Chdir(l.Folder) - acf, err := l.FindACF(g) - if err != nil { - return err - } - input := []string{"common/" + g, acf} - a := archive.Archive{Output: output, Input: input} - err = a.Tar(compress) - if err != nil { - return err - } + twriter := tar.NewWriter(wr) - os.Chdir(working_dir) + for _, pth := range []string{"common/" + g.Name, acf} { + if err := filepath.Walk(pth, TarWalkfn(twriter)); err != nil { + return err + } + } return nil } -func (l *Library) ExtractGameFromFile(f, compress string) error { - working_dir, err := os.Getwd() - if err != nil { +func (l *Library) Extract(r io.Reader) error { + if err := os.Chdir(l.Folder); err != nil { return err } - f, err = filepath.Abs(f) - if err != nil { - return err - } + treader := tar.NewReader(r) - if err = os.Chdir(l.Folder); err != nil { - return err - } - u := &archive.Unarchive{ - Input: f, - } - if err := u.UnTar(compress); err != nil { - return err - } + for { + hdr, err := treader.Next() + if err == io.EOF { + // We've reached the end! Whoee + break + } + if err != nil { + return err + } - if err = os.Chdir(working_dir); err != nil { - return err - } - return nil -} + // Fix windows slashes... + fileName := strings.Replace(hdr.Name, "\\", "/", -1) -func (l *Library) DeleteGame(i int) error { - g := l.Games[i] + info := hdr.FileInfo() + if info.IsDir() { + // I don't like hard-coded permissions but it + // it helps with overall platform compatibility + if err = os.MkdirAll(fileName, 0775); err != nil { + return err + } + continue + } + + if err = os.MkdirAll(filepath.Dir(fileName), 0775); err != nil { + return err + } + + // Create a file handle to work with + f, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0664) + if err != nil { + return err + } + defer f.Close() + if _, err := io.Copy(f, treader); err != nil { + return err + } - working_dir, err := os.Getwd() - if err != nil { - return err } - if err = os.Chdir(l.Folder); err != nil { + return nil +} + +func (g *Game) Delete() error { + if err := os.Chdir(g.LibraryPath); err != nil { return err } - acf, err := l.FindACF(g) + acf, err := FindACF(g.LibraryPath, g.Name) if err != nil { return err } - // fmt.Fprintf(os.Stderr, "Removing %q %q\n", acf, "common/"+g) if err := os.Remove(acf); err != nil { return err } - if err := os.RemoveAll("common/" + g); err != nil { - return err - } - - if err = os.Chdir(working_dir); err != nil { + if err := os.RemoveAll("common/" + g.Name); err != nil { return err } diff --git a/steam/steam.go b/steam/steam.go index ea27e5a..7071a20 100644 --- a/steam/steam.go +++ b/steam/steam.go @@ -5,19 +5,21 @@ package steam import ( "fmt" "io/ioutil" - "errors" - "bufio" "os" "path/filepath" "strings" - // "log" ) type Library struct { Folder string - Games []string + Games map[string]Game +} + +type Game struct { + Name string + LibraryPath string } func ProcessMultipleLibraries(r []string) ([]*Library, error) { @@ -35,30 +37,33 @@ func ProcessMultipleLibraries(r []string) ([]*Library, error) { // Populate the "Folder" and "Games" fields based on the provided directory func (s *Library) ProcessLibrary(r string) error { - if hasCommon(r) { - dirs, err := ioutil.ReadDir(r + "/common") - if err != nil { - return err - } - s.Folder = r - for _, f := range dirs { - if f.IsDir() { - s.Games = append(s.Games, f.Name()) + if !hasCommon(r) { + return errors.New(fmt.Sprintf("No common directory in: %s", r)) + } + + s.Games = make(map[string]Game) + + dirs, err := ioutil.ReadDir(r + "/common") + if err != nil { + return err + } + s.Folder = r + for _, f := range dirs { + if f.IsDir() { + s.Games[f.Name()] = Game{ + Name: f.Name(), + LibraryPath: r, } + // s.Games = append(s.Games, f.Name()) } - } else { - return errors.New(fmt.Sprintf("No common directory in: %s", r)) } + return nil } // Find the ACF files related to this video game -func (l *Library) FindACF(g string) (string, error) { - working_dir, err := os.Getwd() - if err != nil { - return "", err - } - if err = os.Chdir(l.Folder); err != nil { +func FindACF(libraryPath, game string) (string, error) { + if err := os.Chdir(libraryPath); err != nil { return "", err } files, err := filepath.Glob("*.acf") @@ -84,8 +89,7 @@ func (l *Library) FindACF(g string) (string, error) { scanner := bufio.NewScanner(f) for scanner.Scan() { // Finally check and see if the file has the video game name - if strings.Contains(scanner.Text(), g) { - os.Chdir(working_dir) + if strings.Contains(scanner.Text(), game) { return fn, nil // fmt.Printf("%s/%s:%d: %s\n", root, path, i, scanner.Text()) } @@ -93,18 +97,16 @@ func (l *Library) FindACF(g string) (string, error) { } str := "Couldn't find ACF file related to Game: %s" - return "", errors.New(fmt.Sprintf(str, g)) + return "", errors.New(fmt.Sprintf(str, game)) } // This is automatically called to print out the contents of the struct // when things like fmt.Println are used func (s *Library) String() (str string) { - str = fmt.Sprintln("\n----") - str = str + fmt.Sprintln(s.Folder) - str = str + "\n----\n" - for k, v := range s.Games { - str = str + fmt.Sprintf("%d: %s\n", k, v) - //str = str + fmt.Sprintln(v) + str = fmt.Sprintf("Library: %s\n", s.Folder) + str = str + "----\n" + for _, v := range s.Games { + str = str + fmt.Sprintf("%s\n", v.Name) } return } diff --git a/steam/unix.go b/steam/unix.go deleted file mode 100644 index caa4b43..0000000 --- a/steam/unix.go +++ /dev/null @@ -1,10 +0,0 @@ -// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris - -package steam - -import ( - "os" - "path/filepath" -) - -var DefaultLib string = filepath.Join(os.Getenv("HOME"), ".steam/steam/steamapps") diff --git a/steam/windows.go b/steam/windows.go deleted file mode 100644 index 17fe62c..0000000 --- a/steam/windows.go +++ /dev/null @@ -1,5 +0,0 @@ -// +build windows - -package steam - -var DefaultLib string = `C:\Program Files (x86)\Steam\steamapps` |
