Skip to content

Commit

Permalink
Feat (library): optimize library load and steam detection with better…
Browse files Browse the repository at this point in the history
… methods and optional reading from custom environment variables
  • Loading branch information
mateussouzaweb committed Nov 8, 2024
1 parent 136a9e2 commit a7b1d4e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 39 deletions.
60 changes: 21 additions & 39 deletions src/library/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"slices"
"strings"

Expand All @@ -30,53 +29,36 @@ func Load() error {

var err error

// Set default runtime configs
_config = Config{
SteamRuntime: "",
SteamPath: "",
ConfigPath: os.ExpandEnv("$GAMES/NICE"),
ArtworksPath: os.ExpandEnv("$GAMES/NICE/artworks"),
}

// Retrieve Steam base path
steamPath, err := steam.GetPath()
if err != nil {
return fmt.Errorf("could not detect Steam installation: %s", err)
}

// Tweak config based on Steam detection
if steamPath == "" {
cli.Printf(cli.ColorWarn, "Steam installation was not detected.\n")
cli.Printf(cli.ColorWarn, "Please make sure to install and login into Steam first.\n")
} else {

// Check how Steam is running
steamRuntime, err := steam.GetRuntime()
if err != nil {
return fmt.Errorf("could not determine Steam runtime: %s", err)
}

// Retrieve users config path on Steam
// Steam can contains more than one user, but we manage only one
configPaths, err := filepath.Glob(steamPath + "/userdata/*/config")
if err != nil {
return fmt.Errorf("could not detect Steam user configuration: %s", err)
}
if len(configPaths) == 0 {
return fmt.Errorf("could not detect Steam user configuration: please make sure to login into Steam first")
}
// Check how Steam is running
steamRuntime, err := steam.GetRuntime()
if err != nil {
return fmt.Errorf("could not determine Steam runtime: %s", err)
}

// Make sure zero config is ignored (this is not a valid user)
if strings.Contains(configPaths[0], "/0/config") {
configPaths = configPaths[1:]
}
// Retrieve Steam user config path
configPath, err := steam.GetConfigPath()
if err != nil {
return fmt.Errorf("could not detect Steam user config path: %s", err)
}

// Set runtime configs
_config.SteamPath = steamPath
_config.SteamRuntime = steamRuntime
_config.ConfigPath = configPaths[0]
_config.ArtworksPath = configPaths[0] + "/grid"
// Set default runtime configs
_config = Config{
SteamPath: steamPath,
SteamRuntime: steamRuntime,
ConfigPath: configPath,
ArtworksPath: configPath + "/grid",
}

// Show message based on Steam detection
if _config.SteamPath == "" {
cli.Printf(cli.ColorWarn, "Steam installation was not detected.\n")
cli.Printf(cli.ColorWarn, "Please make sure to install and login into Steam first.\n")
}

// Load config file if exist
Expand Down
51 changes: 51 additions & 0 deletions src/steam/steam.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package steam
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/mateussouzaweb/nicedeck/src/cli"
"github.com/mateussouzaweb/nicedeck/src/fs"
Expand All @@ -12,6 +14,12 @@ import (
// Check the runtime that Steam installation is running
func GetRuntime() (string, error) {

// Check from environment variable
fromEnv := cli.GetEnv("STEAM_RUNTIME", "")
if fromEnv != "" {
return fromEnv, nil
}

// Check flatpak --system install
flatpakSystemFile := "/var/lib/flatpak/exports/bin/com.valvesoftware.Steam"
exist, err := fs.FileExist(flatpakSystemFile)
Expand All @@ -36,6 +44,12 @@ func GetRuntime() (string, error) {
// Retrieve the absolute Steam path
func GetPath() (string, error) {

// Check from environment variable
fromEnv := cli.GetEnv("STEAM_PATH", "")
if fromEnv != "" {
return fromEnv, nil
}

// Fill possible locations
paths := []string{
os.ExpandEnv("$HOME/.steam/steam"),
Expand All @@ -57,6 +71,43 @@ func GetPath() (string, error) {
return "", nil
}

// Retrieve Steam user config path
func GetConfigPath() (string, error) {

// Check from environment variable
fromEnv := cli.GetEnv("STEAM_USER_CONFIG_PATH", "")
if fromEnv != "" {
return fromEnv, nil
}

// Retrieve Steam base path
steamPath, err := GetPath()
if err != nil {
return "", fmt.Errorf("could not detect Steam installation: %s", err)
}

// Steam can contains more than one user
// At this time, we manage only the first user
configPaths, err := filepath.Glob(steamPath + "/userdata/*/config")
if err != nil {
return "", fmt.Errorf("could not detect Steam user configuration: %s", err)
}

// Make sure zero config is ignored (this is not a valid user)
if len(configPaths) > 0 {
if strings.Contains(configPaths[0], "/0/config") {
configPaths = configPaths[1:]
}
}

// Check if results was found
if len(configPaths) == 0 {
return "", fmt.Errorf("no users detected, please make sure to login into Steam first")
}

return configPaths[0], nil
}

// Perform Steam setup
func Setup() error {

Expand Down

0 comments on commit a7b1d4e

Please sign in to comment.