aboutsummaryrefslogtreecommitdiff
path: root/lib/steam
diff options
context:
space:
mode:
Diffstat (limited to 'lib/steam')
-rw-r--r--lib/steam/package.go96
-rw-r--r--lib/steam/steam.go123
-rw-r--r--lib/steam/unix.go10
-rw-r--r--lib/steam/windows.go5
4 files changed, 234 insertions, 0 deletions
diff --git a/lib/steam/package.go b/lib/steam/package.go
new file mode 100644
index 0000000..2e25994
--- /dev/null
+++ b/lib/steam/package.go
@@ -0,0 +1,96 @@
+package steam
+
+import (
+ // "fmt"
+ "git.riedstra.us/mitch/steam-export/lib/archive"
+ "os"
+ "path/filepath"
+)
+
+func (l *Library) PackageGameToFile(index int, file, compress string) error {
+ g := l.Games[index]
+
+ working_dir, err := os.Getwd()
+ if err != nil {
+ return err
+ }
+ // output := working_dir + "/" + g + ".tar"
+ output, err := filepath.Abs(file)
+ 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
+ }
+
+ os.Chdir(working_dir)
+
+ return nil
+}
+
+func (l *Library) ExtractGameFromFile(f, compress string) error {
+ working_dir, err := os.Getwd()
+ if err != nil {
+ return err
+ }
+
+ f, err = filepath.Abs(f)
+ if err != nil {
+ return err
+ }
+
+ if err = os.Chdir(l.Folder); err != nil {
+ return err
+ }
+ u := &archive.Unarchive{
+ Input: f,
+ }
+ if err := u.UnTar(compress); err != nil {
+ return err
+ }
+
+ if err = os.Chdir(working_dir); err != nil {
+ return err
+ }
+ return nil
+}
+
+func (l *Library) DeleteGame(i int) error {
+ g := l.Games[i]
+
+ working_dir, err := os.Getwd()
+ if err != nil {
+ return err
+ }
+
+ if err = os.Chdir(l.Folder); err != nil {
+ return err
+ }
+
+ acf, err := l.FindACF(g)
+ 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 {
+ return err
+ }
+
+ return nil
+}
diff --git a/lib/steam/steam.go b/lib/steam/steam.go
new file mode 100644
index 0000000..ea27e5a
--- /dev/null
+++ b/lib/steam/steam.go
@@ -0,0 +1,123 @@
+// This is designed to be a rather simplistic library to help pull information
+// from a local steam library
+package steam
+
+import (
+ "fmt"
+ "io/ioutil"
+
+ "errors"
+
+ "bufio"
+ "os"
+ "path/filepath"
+ "strings"
+ // "log"
+)
+
+type Library struct {
+ Folder string
+ Games []string
+}
+
+func ProcessMultipleLibraries(r []string) ([]*Library, error) {
+ var libs []*Library
+ for _, i := range r {
+ lib := &Library{}
+ err := lib.ProcessLibrary(i)
+ if err != nil {
+ return nil, err
+ }
+ libs = append(libs, lib)
+ }
+ return libs, nil
+}
+
+// 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())
+ }
+ }
+ } 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 {
+ return "", err
+ }
+ files, err := filepath.Glob("*.acf")
+ if err != nil {
+ return "", err
+ }
+ for _, fn := range files {
+ info, err := os.Lstat(fn)
+ if err != nil {
+ return "", err
+ }
+ // We don't want it if it's a directory
+ if info.IsDir() {
+ continue
+ }
+
+ // Open up the file
+ f, err := os.Open(fn)
+ defer f.Close()
+ if err != nil {
+ return "", err
+ }
+ 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)
+ return fn, nil
+ // fmt.Printf("%s/%s:%d: %s\n", root, path, i, scanner.Text())
+ }
+ }
+
+ }
+ str := "Couldn't find ACF file related to Game: %s"
+ return "", errors.New(fmt.Sprintf(str, g))
+}
+
+// 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)
+ }
+ return
+}
+
+func hasCommon(d string) bool {
+ dirs, err := ioutil.ReadDir(d)
+ if err != nil {
+ return false
+ }
+ for _, f := range dirs {
+ if f.Name() == "common" && f.IsDir() {
+ return true
+ }
+ }
+ return false
+}
diff --git a/lib/steam/unix.go b/lib/steam/unix.go
new file mode 100644
index 0000000..caa4b43
--- /dev/null
+++ b/lib/steam/unix.go
@@ -0,0 +1,10 @@
+// +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/lib/steam/windows.go b/lib/steam/windows.go
new file mode 100644
index 0000000..17fe62c
--- /dev/null
+++ b/lib/steam/windows.go
@@ -0,0 +1,5 @@
+// +build windows
+
+package steam
+
+var DefaultLib string = `C:\Program Files (x86)\Steam\steamapps`