Skip to content

Commit

Permalink
Convert install finders tracking to self register
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata committed Apr 17, 2024
1 parent b474ea4 commit 2ab5bc9
Show file tree
Hide file tree
Showing 28 changed files with 336 additions and 281 deletions.
14 changes: 14 additions & 0 deletions backend/installfinders/common/wine_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build unix

package common

import (
"path/filepath"
"strings"
)

func WinePathProcessor(winePrefix string) func(string) string {
return func(path string) string {
return filepath.Join(winePrefix, "dosdevices", strings.ToLower(path[0:1])+strings.ReplaceAll(path[1:], "\\", "/"))
}
}
13 changes: 12 additions & 1 deletion backend/installfinders/findinstalls.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package installfinders

import (
"log/slog"
"strings"

"golang.org/x/exp/maps"

"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/common"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers"

_ "github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers/all" // register all launchers
)

func FindInstallations() ([]*common.Installation, []error) {
return common.FindAll(launchers.GetInstallFinders()...)
registrations := launchers.GetInstallFinders()

slog.Debug("finding installations", slog.String("launchers", strings.Join(maps.Keys(registrations), ",")))

return common.FindAll(maps.Values(registrations)...)
}
9 changes: 9 additions & 0 deletions backend/installfinders/launchers/all/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package all

import (
_ "github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers/crossover" // register crossover
_ "github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers/epic" // register epic
_ "github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers/heroic" // register heroic
_ "github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers/legendary" // register legendary
_ "github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers/steam" // register steam
)
16 changes: 0 additions & 16 deletions backend/installfinders/launchers/all_darwin.go

This file was deleted.

18 changes: 0 additions & 18 deletions backend/installfinders/launchers/all_linux.go

This file was deleted.

18 changes: 0 additions & 18 deletions backend/installfinders/launchers/all_windows.go

This file was deleted.

13 changes: 7 additions & 6 deletions backend/installfinders/launchers/crossover/crossover_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"log/slog"
"os"
"path/filepath"
"strings"

"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/common"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers/steam"
)

Expand All @@ -16,7 +16,11 @@ var (
crossoverSteamPath = filepath.Join("c:", "Program Files (x86)", "Steam") // Will get run through processPath, so it will be added to the dosdevices path
)

func FindInstallations() ([]*common.Installation, []error) {
func init() {
launchers.Add("CrossOver", crossover)
}

func crossover() ([]*common.Installation, []error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, []error{fmt.Errorf("failed to get user home dir: %w", err)}
Expand All @@ -37,10 +41,7 @@ func FindInstallations() ([]*common.Installation, []error) {
continue
}
bottleRoot := filepath.Join(bottlesPath, bottle.Name())

processPath := func(path string) string {
return filepath.Join(bottleRoot, "dosdevices", strings.ToLower(path[0:1])+strings.ReplaceAll(path[1:], "\\", "/"))
}
processPath := common.WinePathProcessor(bottleRoot)

if _, err := os.Stat(processPath(crossoverSteamPath)); os.IsNotExist(err) {
slog.Debug("Skipping bottle without Steam", slog.String("bottle", bottle.Name()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package crossover
2 changes: 1 addition & 1 deletion backend/installfinders/launchers/epic/epic.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func FindInstallationsEpic(epicManifestsPath string, launcher string, launchPath
}

if _, err := os.Stat(epicManifestsPath); os.IsNotExist(err) {
return nil, []error{fmt.Errorf("Epic is not installed")}
return nil, []error{fmt.Errorf("epic is not installed")}
}

manifests, err := os.ReadDir(epicManifestsPath)
Expand Down
27 changes: 15 additions & 12 deletions backend/installfinders/launchers/epic/epic_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import (
"path/filepath"

"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/common"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers"
)

var epicManifestsFolder = filepath.Join(os.Getenv("PROGRAMDATA"), "Epic", "EpicGamesLauncher", "Data", "Manifests")

func FindInstallations() ([]*common.Installation, []error) {
return FindInstallationsEpic(epicManifestsFolder, "Epic Games", func(appName string) []string {
return []string{
"cmd",
"/C",
`start`,
``,
// The extra space at the end is required for exec to escape the argument with double quotes
// Otherwise, the & is interpreted as a command sequence
`com.epicgames.launcher://apps/` + appName + `?action=launch&silent=true `,
}
}, nil)
func init() {
launchers.Add("EpicGames", func() ([]*common.Installation, []error) {
return FindInstallationsEpic(epicManifestsFolder, "Epic Games", func(appName string) []string {
return []string{
"cmd",
"/C",
`start`,
``,
// The extra space at the end is required for exec to escape the argument with double quotes
// Otherwise, the & is interpreted as a command sequence
`com.epicgames.launcher://apps/` + appName + `?action=launch&silent=true `,
}
}, nil)
})
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//go:build unix

package epic

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/common"
)
Expand All @@ -19,7 +20,5 @@ func FindInstallationsWine(winePrefix string, launcher string, launchPath []stri
return nil, []error{fmt.Errorf("Epic is not installed in " + winePrefix)}
}

return FindInstallationsEpic(epicManifestsPath, launcher, func(appName string) []string { return launchPath }, func(path string) string {
return filepath.Join(wineWindowsRoot, strings.ToLower(path[0:1])+strings.ReplaceAll(path[1:], "\\", "/"))
})
return FindInstallationsEpic(epicManifestsPath, launcher, func(appName string) []string { return launchPath }, common.WinePathProcessor(winePrefix))
}
9 changes: 0 additions & 9 deletions backend/installfinders/launchers/heroic/heroic_darwin.go

This file was deleted.

22 changes: 22 additions & 0 deletions backend/installfinders/launchers/heroic/heroic_flatpak_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package heroic

import (
"fmt"
"os"
"path/filepath"

"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/common"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers"
)

func init() {
launchers.Add("Heroic-flatpak", func() ([]*common.Installation, []error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, []error{fmt.Errorf("failed to get user home dir: %w", err)}
}
flatpakXdgConfigHome := filepath.Join(homeDir, ".var", "app", "com.heroicgameslauncher.hgl", "config")

return findInstallationsHeroic(false, flatpakXdgConfigHome, "Heroic")
})
}
67 changes: 0 additions & 67 deletions backend/installfinders/launchers/heroic/heroic_linux.go

This file was deleted.

12 changes: 12 additions & 0 deletions backend/installfinders/launchers/heroic/heroic_native_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package heroic

import (
"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/common"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers"
)

func init() {
launchers.Add("Heroic", func() ([]*common.Installation, []error) {
return findInstallationsHeroic(false, "", "Heroic")
})
}
61 changes: 61 additions & 0 deletions backend/installfinders/launchers/heroic/heroic_snap_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package heroic

import (
"fmt"
"os"
"path/filepath"
"strconv"

"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/common"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/launchers"
)

func init() {
launchers.Add("Heroic-snap", func() ([]*common.Installation, []error) {
snapPath, err := getSnapPath()
if err != nil {
return nil, []error{fmt.Errorf("failed to get snap path: %w", err)}
}

return findInstallationsHeroic(true, filepath.Join(snapPath, ".config"), "Heroic")
})
}

func getSnapPath() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user home dir: %w", err)
}
snapAppDir := filepath.Join(homeDir, "snap", "heroic")
var latestSnapRevision int
var latestSnapDirName string
items, err := os.ReadDir(snapAppDir)
if err != nil {
return "", fmt.Errorf("failed to read heroic snap dir: %w", err)
}
for _, item := range items {
if item.IsDir() {
folderName := item.Name()
var revision int
if folderName[0] == 'x' {
revision, err = strconv.Atoi(folderName[1:])
if err != nil {
continue
}
} else {
revision, err = strconv.Atoi(folderName)
if err != nil {
continue
}
}
if latestSnapDirName == "" || revision > latestSnapRevision {
latestSnapRevision = revision
latestSnapDirName = folderName
}
}
}
if latestSnapDirName == "" {
return "", fmt.Errorf("no heroic snap folders found")
}
return filepath.Join(snapAppDir, latestSnapDirName), nil
}
9 changes: 0 additions & 9 deletions backend/installfinders/launchers/heroic/heroic_windows.go

This file was deleted.

Loading

0 comments on commit 2ab5bc9

Please sign in to comment.