From a7b1d4e67010f789fb4de653add95f578b72b4fb Mon Sep 17 00:00:00 2001 From: Mateus Souza Date: Fri, 8 Nov 2024 01:14:35 +0000 Subject: [PATCH] Feat (library): optimize library load and steam detection with better methods and optional reading from custom environment variables --- src/library/config.go | 60 +++++++++++++++---------------------------- src/steam/steam.go | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 39 deletions(-) diff --git a/src/library/config.go b/src/library/config.go index e853c46..35cf08a 100644 --- a/src/library/config.go +++ b/src/library/config.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "os" - "path/filepath" "slices" "strings" @@ -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 diff --git a/src/steam/steam.go b/src/steam/steam.go index 4521a47..186bdc5 100644 --- a/src/steam/steam.go +++ b/src/steam/steam.go @@ -3,6 +3,8 @@ package steam import ( "fmt" "os" + "path/filepath" + "strings" "github.com/mateussouzaweb/nicedeck/src/cli" "github.com/mateussouzaweb/nicedeck/src/fs" @@ -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) @@ -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"), @@ -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 {