2021-11-05 21:42:49 +00:00
|
|
|
package cli
|
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-06-22 22:24:35 +00:00
|
|
|
"net/url"
|
2021-12-02 04:00:33 +00:00
|
|
|
"os"
|
2022-05-02 20:07:15 +00:00
|
|
|
"path/filepath"
|
2022-06-03 22:17:02 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2022-06-18 16:09:09 +00:00
|
|
|
"sync"
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/viper"
|
2023-12-06 23:39:34 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-10-14 16:11:16 +00:00
|
|
|
|
2023-12-06 04:47:41 +00:00
|
|
|
"github.com/satisfactorymodding/ficsit-cli/cli/cache"
|
2022-10-14 16:11:16 +00:00
|
|
|
"github.com/satisfactorymodding/ficsit-cli/cli/disk"
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/utils"
|
2021-12-02 04:00:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InstallationsVersion int
|
|
|
|
|
|
|
|
const (
|
|
|
|
InitialInstallationsVersion = InstallationsVersion(iota)
|
|
|
|
|
|
|
|
// Always last
|
|
|
|
nextInstallationsVersion
|
|
|
|
)
|
|
|
|
|
|
|
|
type Installations struct {
|
|
|
|
SelectedInstallation string `json:"selected_installation"`
|
2022-10-14 16:11:16 +00:00
|
|
|
Installations []*Installation `json:"installations"`
|
|
|
|
Version InstallationsVersion `json:"version"`
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 21:42:49 +00:00
|
|
|
type Installation struct {
|
2022-10-14 16:11:16 +00:00
|
|
|
DiskInstance disk.Disk `json:"-"`
|
2022-06-22 22:24:35 +00:00
|
|
|
Path string `json:"path"`
|
|
|
|
Profile string `json:"profile"`
|
2023-12-06 04:02:06 +00:00
|
|
|
Vanilla bool `json:"vanilla"`
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitInstallations() (*Installations, error) {
|
2022-04-14 01:27:39 +00:00
|
|
|
localDir := viper.GetString("local-dir")
|
2021-12-02 04:00:33 +00:00
|
|
|
|
2022-06-05 01:56:46 +00:00
|
|
|
installationsFile := filepath.Join(localDir, viper.GetString("installations-file"))
|
2021-12-02 04:00:33 +00:00
|
|
|
_, err := os.Stat(installationsFile)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, errors.Wrap(err, "failed to stat installations file")
|
|
|
|
}
|
|
|
|
|
2022-04-14 01:27:39 +00:00
|
|
|
_, err := os.Stat(localDir)
|
2021-12-02 04:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, errors.Wrap(err, "failed to read cache directory")
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:11:16 +00:00
|
|
|
err = os.MkdirAll(localDir, 0o755)
|
2021-12-02 04:00:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create cache directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyInstallations := Installations{
|
|
|
|
Version: nextInstallationsVersion - 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := emptyInstallations.Save(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to save empty installations")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
installationsData, err := os.ReadFile(installationsFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to read installations")
|
|
|
|
}
|
|
|
|
|
|
|
|
var installations Installations
|
|
|
|
if err := json.Unmarshal(installationsData, &installations); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to unmarshal installations")
|
|
|
|
}
|
|
|
|
|
|
|
|
if installations.Version >= nextInstallationsVersion {
|
|
|
|
return nil, fmt.Errorf("unknown installations version: %d", installations.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &installations, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installations) Save() error {
|
|
|
|
if viper.GetBool("dry-run") {
|
|
|
|
log.Info().Msg("dry-run: skipping installation saving")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-05 01:56:46 +00:00
|
|
|
installationsFile := filepath.Join(viper.GetString("local-dir"), viper.GetString("installations-file"))
|
2021-12-02 04:00:33 +00:00
|
|
|
|
|
|
|
log.Info().Str("path", installationsFile).Msg("saving installations")
|
|
|
|
|
|
|
|
installationsJSON, err := json.MarshalIndent(i, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal installations")
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:11:16 +00:00
|
|
|
if err := os.WriteFile(installationsFile, installationsJSON, 0o755); err != nil {
|
2021-12-02 04:00:33 +00:00
|
|
|
return errors.Wrap(err, "failed to write installations")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installations) AddInstallation(ctx *GlobalContext, installPath string, profile string) (*Installation, error) {
|
2022-06-22 22:24:35 +00:00
|
|
|
parsed, err := url.Parse(installPath)
|
2022-05-02 20:07:15 +00:00
|
|
|
if err != nil {
|
2022-06-22 22:24:35 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to parse path")
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:11:16 +00:00
|
|
|
absolutePath := installPath
|
2022-06-22 22:37:36 +00:00
|
|
|
if parsed.Scheme != "ftp" && parsed.Scheme != "sftp" {
|
2022-06-22 22:24:35 +00:00
|
|
|
absolutePath, err = filepath.Abs(installPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not resolve absolute path of: "+installPath)
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
installation := &Installation{
|
2022-05-02 20:07:15 +00:00
|
|
|
Path: absolutePath,
|
2021-12-02 04:00:33 +00:00
|
|
|
Profile: profile,
|
2023-12-06 04:02:06 +00:00
|
|
|
Vanilla: false,
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := installation.Validate(ctx); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to validate installation")
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
2022-04-14 01:27:39 +00:00
|
|
|
for _, install := range i.Installations {
|
2022-06-22 22:24:35 +00:00
|
|
|
if filepath.Clean(installation.Path) == filepath.Clean(install.Path) {
|
|
|
|
found = true
|
2021-12-02 04:00:33 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
return nil, errors.New("installation already present")
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Installations = append(i.Installations, installation)
|
|
|
|
|
|
|
|
return installation, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installations) GetInstallation(installPath string) *Installation {
|
|
|
|
for _, install := range i.Installations {
|
|
|
|
if install.Path == installPath {
|
|
|
|
return install
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-14 01:27:39 +00:00
|
|
|
func (i *Installations) DeleteInstallation(installPath string) error {
|
|
|
|
found := -1
|
|
|
|
for j, install := range i.Installations {
|
|
|
|
if install.Path == installPath {
|
|
|
|
found = j
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found == -1 {
|
|
|
|
return errors.New("installation not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Installations = append(i.Installations[:found], i.Installations[found+1:]...)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
func (i *Installation) Validate(ctx *GlobalContext) error {
|
|
|
|
found := false
|
|
|
|
for _, p := range ctx.Profiles.Profiles {
|
|
|
|
if p.Name == i.Profile {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return errors.New("profile not found")
|
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
d, err := i.GetDisk()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-14 01:27:39 +00:00
|
|
|
foundExecutable := false
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
err = d.Exists(filepath.Join(i.BasePath(), "FactoryGame.exe"))
|
2022-04-14 01:27:39 +00:00
|
|
|
if err != nil {
|
2022-06-22 22:24:35 +00:00
|
|
|
if !d.IsNotExist(err) {
|
2022-04-14 01:27:39 +00:00
|
|
|
return errors.Wrap(err, "failed reading FactoryGame.exe")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foundExecutable = true
|
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
err = d.Exists(filepath.Join(i.BasePath(), "FactoryServer.sh"))
|
2022-04-14 01:27:39 +00:00
|
|
|
if err != nil {
|
2022-06-22 22:24:35 +00:00
|
|
|
if !d.IsNotExist(err) {
|
2022-04-14 01:27:39 +00:00
|
|
|
return errors.Wrap(err, "failed reading FactoryServer.sh")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foundExecutable = true
|
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
err = d.Exists(filepath.Join(i.BasePath(), "FactoryServer.exe"))
|
2022-04-14 01:27:39 +00:00
|
|
|
if err != nil {
|
2022-06-22 22:24:35 +00:00
|
|
|
if !d.IsNotExist(err) {
|
2022-04-14 01:27:39 +00:00
|
|
|
return errors.Wrap(err, "failed reading FactoryServer.exe")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foundExecutable = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !foundExecutable {
|
2022-06-22 22:24:35 +00:00
|
|
|
return errors.New("did not find game executable in " + i.BasePath())
|
2022-04-14 01:27:39 +00:00
|
|
|
}
|
2021-12-02 04:00:33 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
var (
|
|
|
|
lockFileCleaner = regexp.MustCompile(`[^a-zA-Z\d]]`)
|
|
|
|
matchFirstCap = regexp.MustCompile(`(.)([A-Z][a-z]+)`)
|
|
|
|
matchAllCap = regexp.MustCompile(`([a-z\d])([A-Z])`)
|
|
|
|
)
|
2021-12-02 04:00:33 +00:00
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
func (i *Installation) LockFilePath(ctx *GlobalContext) (string, error) {
|
2022-05-02 20:07:15 +00:00
|
|
|
platform, err := i.GetPlatform(ctx)
|
|
|
|
if err != nil {
|
2022-06-03 22:17:02 +00:00
|
|
|
return "", err
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
lockFileName := ctx.Profiles.Profiles[i.Profile].Name
|
|
|
|
lockFileName = matchFirstCap.ReplaceAllString(lockFileName, "${1}_${2}")
|
|
|
|
lockFileName = matchAllCap.ReplaceAllString(lockFileName, "${1}_${2}")
|
|
|
|
lockFileName = lockFileCleaner.ReplaceAllLiteralString(lockFileName, "-")
|
|
|
|
lockFileName = strings.ToLower(lockFileName) + "-lock.json"
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
return filepath.Join(i.BasePath(), platform.LockfilePath, lockFileName), nil
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installation) LockFile(ctx *GlobalContext) (*LockFile, error) {
|
|
|
|
lockfilePath, err := i.LockFilePath(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
d, err := i.GetDisk()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
var lockFile *LockFile
|
2022-06-22 22:24:35 +00:00
|
|
|
lockFileJSON, err := d.Read(lockfilePath)
|
2022-05-02 20:07:15 +00:00
|
|
|
if err != nil {
|
2022-06-22 22:24:35 +00:00
|
|
|
if !d.IsNotExist(err) {
|
2022-06-03 22:17:02 +00:00
|
|
|
return nil, errors.Wrap(err, "failed reading lockfile")
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := json.Unmarshal(lockFileJSON, &lockFile); err != nil {
|
2022-06-03 22:17:02 +00:00
|
|
|
return nil, errors.Wrap(err, "failed parsing lockfile")
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
return lockFile, nil
|
|
|
|
}
|
|
|
|
|
2023-12-07 16:57:31 +00:00
|
|
|
func (i *Installation) WriteLockFile(ctx *GlobalContext, lockfile *LockFile) error {
|
2022-06-03 22:17:02 +00:00
|
|
|
lockfilePath, err := i.LockFilePath(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-06 04:02:06 +00:00
|
|
|
d, err := i.GetDisk()
|
2022-06-03 22:17:02 +00:00
|
|
|
if err != nil {
|
2023-12-06 04:02:06 +00:00
|
|
|
return err
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 04:02:06 +00:00
|
|
|
lockfileDir := filepath.Dir(lockfilePath)
|
|
|
|
if err := d.Exists(lockfileDir); d.IsNotExist(err) {
|
|
|
|
if err := d.MkDir(lockfileDir); err != nil {
|
|
|
|
return errors.Wrap(err, "failed creating lockfile directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
marshaledLockfile, err := json.MarshalIndent(lockfile, "", " ")
|
2022-06-22 22:24:35 +00:00
|
|
|
if err != nil {
|
2023-12-06 04:02:06 +00:00
|
|
|
return errors.Wrap(err, "failed to serialize lockfile json")
|
2022-06-22 22:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Write(lockfilePath, marshaledLockfile); err != nil {
|
2022-06-03 22:17:02 +00:00
|
|
|
return errors.Wrap(err, "failed writing lockfile")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-06 19:37:33 +00:00
|
|
|
func (i *Installation) Wipe() error {
|
|
|
|
d, err := i.GetDisk()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
modsDirectory := filepath.Join(i.BasePath(), "FactoryGame", "Mods")
|
|
|
|
if err := d.Remove(modsDirectory); err != nil {
|
|
|
|
return errors.Wrap(err, "failed removing Mods directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-07 16:57:31 +00:00
|
|
|
func (i *Installation) ResolveProfile(ctx *GlobalContext) (*LockFile, error) {
|
2023-12-06 04:02:06 +00:00
|
|
|
lockFile, err := i.LockFile(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-06 04:47:41 +00:00
|
|
|
resolver := NewDependencyResolver(ctx.Provider)
|
2023-12-06 04:02:06 +00:00
|
|
|
|
|
|
|
gameVersion, err := i.GetGameVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to detect game version")
|
|
|
|
}
|
|
|
|
|
|
|
|
lockfile, err := ctx.Profiles.Profiles[i.Profile].Resolve(resolver, lockFile, gameVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not resolve mods")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.WriteLockFile(ctx, lockfile); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to write lockfile")
|
|
|
|
}
|
|
|
|
|
|
|
|
return lockfile, nil
|
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
type InstallUpdateType string
|
|
|
|
|
|
|
|
var (
|
|
|
|
InstallUpdateTypeOverall InstallUpdateType = "overall"
|
|
|
|
InstallUpdateTypeModDownload InstallUpdateType = "download"
|
|
|
|
InstallUpdateTypeModExtract InstallUpdateType = "extract"
|
|
|
|
InstallUpdateTypeModComplete InstallUpdateType = "complete"
|
|
|
|
)
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
type InstallUpdate struct {
|
2023-12-06 23:39:34 +00:00
|
|
|
Type InstallUpdateType
|
|
|
|
Item InstallUpdateItem
|
|
|
|
Progress utils.GenericProgress
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
type InstallUpdateItem struct {
|
|
|
|
Mod string
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installation) Install(ctx *GlobalContext, updates chan<- InstallUpdate) error {
|
2022-06-03 22:17:02 +00:00
|
|
|
if err := i.Validate(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to validate installation")
|
|
|
|
}
|
|
|
|
|
2023-12-07 16:57:31 +00:00
|
|
|
platform, err := i.GetPlatform(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to detect platform")
|
|
|
|
}
|
|
|
|
|
|
|
|
lockfile := MakeLockfile()
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2023-12-06 04:02:06 +00:00
|
|
|
if !i.Vanilla {
|
|
|
|
var err error
|
|
|
|
lockfile, err = i.ResolveProfile(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to resolve lockfile")
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
d, err := i.GetDisk()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
modsDirectory := filepath.Join(i.BasePath(), "FactoryGame", "Mods")
|
|
|
|
if err := d.MkDir(modsDirectory); err != nil {
|
2022-05-02 20:07:15 +00:00
|
|
|
return errors.Wrap(err, "failed creating Mods directory")
|
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
dir, err := d.ReadDir(modsDirectory)
|
2022-06-03 22:17:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to read mods directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range dir {
|
|
|
|
if entry.IsDir() {
|
2023-12-07 16:57:31 +00:00
|
|
|
if _, ok := lockfile.Mods[entry.Name()]; !ok {
|
2022-06-07 23:36:28 +00:00
|
|
|
modDir := filepath.Join(modsDirectory, entry.Name())
|
2022-06-22 22:24:35 +00:00
|
|
|
err := d.Exists(filepath.Join(modDir, ".smm"))
|
2022-06-07 23:36:28 +00:00
|
|
|
if err == nil {
|
|
|
|
log.Info().Str("mod_reference", entry.Name()).Msg("deleting mod")
|
2022-06-22 22:24:35 +00:00
|
|
|
if err := d.Remove(modDir); err != nil {
|
2022-06-07 23:36:28 +00:00
|
|
|
return errors.Wrap(err, "failed to delete mod directory")
|
|
|
|
}
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
log.Info().Int("concurrency", viper.GetInt("concurrent-downloads")).Str("path", i.Path).Msg("starting installation")
|
2022-06-18 16:09:09 +00:00
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
errg := errgroup.Group{}
|
|
|
|
channelUsers := sync.WaitGroup{}
|
|
|
|
downloadSemaphore := make(chan int, viper.GetInt("concurrent-downloads"))
|
|
|
|
defer close(downloadSemaphore)
|
2022-06-18 16:09:09 +00:00
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
var modComplete chan int
|
|
|
|
if updates != nil {
|
|
|
|
channelUsers.Add(1)
|
|
|
|
modComplete = make(chan int)
|
|
|
|
defer close(modComplete)
|
2022-06-18 16:09:09 +00:00
|
|
|
go func() {
|
2023-12-06 23:39:34 +00:00
|
|
|
defer channelUsers.Done()
|
|
|
|
completed := 0
|
|
|
|
for range modComplete {
|
|
|
|
completed++
|
|
|
|
overallUpdate := InstallUpdate{
|
|
|
|
Type: InstallUpdateTypeOverall,
|
|
|
|
Progress: utils.GenericProgress{
|
|
|
|
Completed: int64(completed),
|
2023-12-07 16:57:31 +00:00
|
|
|
Total: int64(len(lockfile.Mods)),
|
2023-12-06 23:39:34 +00:00
|
|
|
},
|
2022-06-18 16:09:09 +00:00
|
|
|
}
|
2023-12-06 23:39:34 +00:00
|
|
|
updates <- overallUpdate
|
2022-06-18 16:09:09 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-12-07 16:57:31 +00:00
|
|
|
for modReference, version := range lockfile.Mods {
|
2023-12-06 23:39:34 +00:00
|
|
|
channelUsers.Add(1)
|
|
|
|
modReference := modReference
|
|
|
|
version := version
|
|
|
|
errg.Go(func() error {
|
|
|
|
defer channelUsers.Done()
|
2023-12-07 16:57:31 +00:00
|
|
|
|
|
|
|
target, ok := version.Targets[platform.TargetName]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("%s@%s not available for %s", modReference, version.Version, platform.TargetName)
|
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
// Only install if a link is provided, otherwise assume mod is already installed
|
2023-12-07 16:57:31 +00:00
|
|
|
if target.Link != "" {
|
|
|
|
err := downloadAndExtractMod(modReference, version.Version, target.Link, target.Hash, modsDirectory, updates, downloadSemaphore, d)
|
2023-12-06 23:39:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to install %s@%s", modReference, version.Version)
|
|
|
|
}
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
if modComplete != nil {
|
|
|
|
modComplete <- 1
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
2023-12-06 23:39:34 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
if updates != nil {
|
|
|
|
go func() {
|
|
|
|
channelUsers.Wait()
|
|
|
|
close(updates)
|
|
|
|
}()
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
if err := errg.Wait(); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to install mods")
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 04:02:06 +00:00
|
|
|
return nil
|
2022-04-14 01:27:39 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 19:37:33 +00:00
|
|
|
func (i *Installation) UpdateMods(ctx *GlobalContext, mods []string) error {
|
|
|
|
if err := i.Validate(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to validate installation")
|
|
|
|
}
|
|
|
|
|
|
|
|
lockFile, err := i.LockFile(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to read lock file")
|
|
|
|
}
|
|
|
|
|
|
|
|
resolver := NewDependencyResolver(ctx.Provider)
|
|
|
|
|
|
|
|
gameVersion, err := i.GetGameVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to detect game version")
|
|
|
|
}
|
|
|
|
|
|
|
|
profile := ctx.Profiles.GetProfile(i.Profile)
|
|
|
|
if profile == nil {
|
|
|
|
return errors.New("could not find profile " + i.Profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, modReference := range mods {
|
|
|
|
lockFile = lockFile.Remove(modReference)
|
|
|
|
}
|
|
|
|
|
|
|
|
newLockFile, err := profile.Resolve(resolver, lockFile, gameVersion)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to resolve dependencies")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.WriteLockFile(ctx, newLockFile); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to write lock file")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
func downloadAndExtractMod(modReference string, version string, link string, hash string, modsDirectory string, updates chan<- InstallUpdate, downloadSemaphore chan int, d disk.Disk) error {
|
|
|
|
var downloadUpdates chan utils.GenericProgress
|
|
|
|
|
|
|
|
if updates != nil {
|
|
|
|
// Forward the inner updates as InstallUpdates
|
|
|
|
downloadUpdates = make(chan utils.GenericProgress)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for up := range downloadUpdates {
|
|
|
|
updates <- InstallUpdate{
|
|
|
|
Item: InstallUpdateItem{
|
|
|
|
Mod: modReference,
|
|
|
|
Version: version,
|
|
|
|
},
|
|
|
|
Type: InstallUpdateTypeModDownload,
|
|
|
|
Progress: up,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Str("mod_reference", modReference).Str("version", version).Str("link", link).Msg("downloading mod")
|
|
|
|
reader, size, err := cache.DownloadOrCache(modReference+"_"+version+".zip", hash, link, downloadUpdates, downloadSemaphore)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to download "+modReference+" from: "+link)
|
|
|
|
}
|
|
|
|
|
2023-12-07 16:57:31 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
var extractUpdates chan utils.GenericProgress
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
if updates != nil {
|
|
|
|
// Forward the inner updates as InstallUpdates
|
|
|
|
extractUpdates = make(chan utils.GenericProgress)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for up := range extractUpdates {
|
2023-12-07 16:57:31 +00:00
|
|
|
updates <- InstallUpdate{
|
2023-12-06 23:39:34 +00:00
|
|
|
Item: InstallUpdateItem{
|
|
|
|
Mod: modReference,
|
|
|
|
Version: version,
|
|
|
|
},
|
|
|
|
Type: InstallUpdateTypeModExtract,
|
|
|
|
Progress: up,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Str("mod_reference", modReference).Str("version", version).Str("link", link).Msg("extracting mod")
|
|
|
|
if err := utils.ExtractMod(reader, size, filepath.Join(modsDirectory, modReference), hash, extractUpdates, d); err != nil {
|
|
|
|
return errors.Wrap(err, "could not extract "+modReference)
|
|
|
|
}
|
|
|
|
|
|
|
|
if updates != nil {
|
2023-12-07 16:57:31 +00:00
|
|
|
updates <- InstallUpdate{
|
2023-12-06 23:39:34 +00:00
|
|
|
Type: InstallUpdateTypeModComplete,
|
|
|
|
Item: InstallUpdateItem{
|
|
|
|
Mod: modReference,
|
|
|
|
Version: version,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
close(extractUpdates)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-14 01:27:39 +00:00
|
|
|
func (i *Installation) SetProfile(ctx *GlobalContext, profile string) error {
|
|
|
|
found := false
|
|
|
|
for _, p := range ctx.Profiles.Profiles {
|
|
|
|
if p.Name == profile {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return errors.New("could not find profile: " + profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Profile = profile
|
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
return nil
|
2021-11-05 21:42:49 +00:00
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
|
|
|
|
type gameVersionFile struct {
|
2022-10-14 16:11:16 +00:00
|
|
|
BranchName string `json:"BranchName"`
|
|
|
|
BuildID string `json:"BuildId"`
|
2022-05-02 20:07:15 +00:00
|
|
|
MajorVersion int `json:"MajorVersion"`
|
|
|
|
MinorVersion int `json:"MinorVersion"`
|
|
|
|
PatchVersion int `json:"PatchVersion"`
|
|
|
|
Changelist int `json:"Changelist"`
|
|
|
|
CompatibleChangelist int `json:"CompatibleChangelist"`
|
|
|
|
IsLicenseeVersion int `json:"IsLicenseeVersion"`
|
|
|
|
IsPromotedBuild int `json:"IsPromotedBuild"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installation) GetGameVersion(ctx *GlobalContext) (int, error) {
|
|
|
|
if err := i.Validate(ctx); err != nil {
|
|
|
|
return 0, errors.Wrap(err, "failed to validate installation")
|
|
|
|
}
|
|
|
|
|
|
|
|
platform, err := i.GetPlatform(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
d, err := i.GetDisk()
|
2022-05-02 20:07:15 +00:00
|
|
|
if err != nil {
|
2022-06-22 22:24:35 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath := filepath.Join(i.BasePath(), platform.VersionPath)
|
|
|
|
file, err := d.Read(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
if d.IsNotExist(err) {
|
2022-05-02 20:07:15 +00:00
|
|
|
return 0, errors.Wrap(err, "could not find game version file")
|
|
|
|
}
|
|
|
|
return 0, errors.Wrap(err, "failed reading version file")
|
|
|
|
}
|
|
|
|
|
|
|
|
var versionData gameVersionFile
|
|
|
|
if err := json.Unmarshal(file, &versionData); err != nil {
|
|
|
|
return 0, errors.Wrap(err, "failed to parse version file json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return versionData.Changelist, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installation) GetPlatform(ctx *GlobalContext) (*Platform, error) {
|
|
|
|
if err := i.Validate(ctx); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to validate installation")
|
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
d, err := i.GetDisk()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
for _, platform := range platforms {
|
2022-06-22 22:24:35 +00:00
|
|
|
fullPath := filepath.Join(i.BasePath(), platform.VersionPath)
|
|
|
|
err := d.Exists(fullPath)
|
2022-05-02 20:07:15 +00:00
|
|
|
if err != nil {
|
2022-06-22 22:24:35 +00:00
|
|
|
if d.IsNotExist(err) {
|
2022-05-02 20:07:15 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-12-06 03:01:49 +00:00
|
|
|
return nil, errors.Wrap(err, "failed detecting version file")
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
return &platform, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("no platform detected")
|
|
|
|
}
|
2022-06-22 22:24:35 +00:00
|
|
|
|
|
|
|
func (i *Installation) GetDisk() (disk.Disk, error) {
|
|
|
|
if i.DiskInstance != nil {
|
|
|
|
return i.DiskInstance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
i.DiskInstance, err = disk.FromPath(i.Path)
|
|
|
|
return i.DiskInstance, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installation) BasePath() string {
|
|
|
|
parsed, err := url.Parse(i.Path)
|
|
|
|
if err != nil {
|
|
|
|
return i.Path
|
|
|
|
}
|
2022-06-22 23:05:35 +00:00
|
|
|
|
|
|
|
if parsed.Scheme != "ftp" && parsed.Scheme != "sftp" {
|
|
|
|
return i.Path
|
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
return parsed.Path
|
|
|
|
}
|