package main
import (
"html/template"
"sync"
"time"
"riedstra.dev/mitch/steam-export/steam"
)
// steamLib is a steam libary with an embeded mutex to protect it
type steamLib struct {
steam.Library
sync.Mutex
}
// statusInfo represents the internal status of game installation
type statusInfo struct {
sync.RWMutex
Running bool
Error error
Url string
Transferred int64
Size int64
Start *time.Time
}
// App binds together the steam library, templates, and channel for install
// requests as well as most the app specific http handlers.
type App struct {
Library *steamLib
Status *statusInfo
Demo bool
Templates *template.Template
// Sending to this channel triggers the downloader in the background
download chan string
}
// NewApp sets up the steam library for us as well as parses the embedded
// template
func NewApp(libPath string) (*App, error) {
lib, err := steam.NewLibrary(libPath)
if err != nil {
return nil, err
}
a := &App{
Library: &steamLib{},
Status: &statusInfo{},
download: make(chan string),
}
a.Library.Library = *lib
a.Templates = template.Must(template.New("index").Parse(indexTemplate))
return a, nil
}
// LibrarySet takes care of locking the Library and switching over to a new
// path, unlocking when done.
// Errors will be logged and no changes will be made if unsuccessful.
func (a *App) LibrarySet(path string) {
Logger.Println("Starting library reload")
a.Library.Lock()
defer a.Library.Unlock()
var err error
l2, err := steam.NewLibrary(path)
if err != nil {
Logger.Printf("Error reopening lib: %s", err)
return
}
a.Library.Library = *l2
Logger.Println("Done reloading lib")
}
// LibraryReload calls LibrarySet but with the current directory, forcing a reload of
// information off of disk.
func (a *App) LibraryReload() {
cur := a.Library.Folder
a.LibrarySet(cur)
return
}