Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vanilla toggle per install #13

Merged
merged 7 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 46 additions & 19 deletions cli/installations.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Installation struct {
DiskInstance disk.Disk `json:"-"`
Path string `json:"path"`
Profile string `json:"profile"`
Vanilla bool `json:"vanilla"`
}

func InitInstallations() (*Installations, error) {
Expand Down Expand Up @@ -127,6 +128,7 @@ func (i *Installations) AddInstallation(ctx *GlobalContext, installPath string,
installation := &Installation{
Path: absolutePath,
Profile: profile,
Vanilla: false,
}

if err := installation.Validate(ctx); err != nil {
Expand Down Expand Up @@ -285,14 +287,21 @@ func (i *Installation) WriteLockFile(ctx *GlobalContext, lockfile LockFile) erro
return err
}

marshaledLockfile, err := json.MarshalIndent(lockfile, "", " ")
d, err := i.GetDisk()
if err != nil {
return errors.Wrap(err, "failed to serialize lockfile json")
return err
}

d, err := i.GetDisk()
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, "", " ")
if err != nil {
return err
return errors.Wrap(err, "failed to serialize lockfile json")
}

if err := d.Write(lockfilePath, marshaledLockfile); err != nil {
Expand All @@ -302,6 +311,31 @@ func (i *Installation) WriteLockFile(ctx *GlobalContext, lockfile LockFile) erro
return nil
}

func (i *Installation) ResolveProfile(ctx *GlobalContext) (LockFile, error) {
lockFile, err := i.LockFile(ctx)
if err != nil {
return nil, err
}

resolver := NewDependencyResolver(ctx.APIClient)

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
}

type InstallUpdate struct {
ModName string
OverallProgress float64
Expand All @@ -314,21 +348,14 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
return errors.Wrap(err, "failed to validate installation")
}

lockFile, err := i.LockFile(ctx)
if err != nil {
return err
}

resolver := NewDependencyResolver(ctx.APIClient)

gameVersion, err := i.GetGameVersion(ctx)
if err != nil {
return errors.Wrap(err, "failed to detect game version")
}
lockfile := make(LockFile)

lockfile, err := ctx.Profiles.Profiles[i.Profile].Resolve(resolver, lockFile, gameVersion)
if err != nil {
return errors.Wrap(err, "could not resolve mods")
if !i.Vanilla {
var err error
lockfile, err = i.ResolveProfile(ctx)
if err != nil {
return errors.Wrap(err, "failed to resolve lockfile")
}
}

d, err := i.GetDisk()
Expand Down Expand Up @@ -435,7 +462,7 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
completed++
}

return i.WriteLockFile(ctx, lockfile)
return nil
}

func (i *Installation) SetProfile(ctx *GlobalContext, profile string) error {
Expand Down
4 changes: 4 additions & 0 deletions cli/installations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ func TestAddInstallation(t *testing.T) {

err = installation.Install(ctx, nil)
testza.AssertNoError(t, err)

installation.Vanilla = true
err = installation.Install(ctx, nil)
testza.AssertNoError(t, err)
}
}
46 changes: 46 additions & 0 deletions cmd/installation/set-vanilla.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package installation

import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/satisfactorymodding/ficsit-cli/cli"
)

func init() {
setVanillaCmd.Flags().BoolP("off", "o", false, "Disable vanilla")

Cmd.AddCommand(setVanillaCmd)
}

var setVanillaCmd = &cobra.Command{
Use: "set-vanilla <path>",
Short: "Set the installation to vanilla mode or not",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
_ = viper.BindPFlag("off", cmd.Flags().Lookup("off"))
},
RunE: func(cmd *cobra.Command, args []string) error {
global, err := cli.InitCLI(false)
if err != nil {
return err
}

var installation *cli.Installation
for _, install := range global.Installations.Installations {
if install.Path == args[0] {
installation = install
break
}
}

if installation == nil {
return errors.New("installation not found")
}

installation.Vanilla = !viper.GetBool("off")

return global.Save()
},
}
12 changes: 12 additions & 0 deletions tea/components/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func (h headerComponent) View() string {
} else {
out += "None"
}
out += "\n"

out += h.labelStyle.Render("Vanilla: ")
if h.root.GetCurrentInstallation() != nil {
if h.root.GetCurrentInstallation().Vanilla {
out += "On"
} else {
out += "Off"
}
} else {
out += "N/A"
}

return lipgloss.NewStyle().Margin(1, 0).Render(out)
}
7 changes: 7 additions & 0 deletions tea/scenes/main_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func NewMainMenu(root components.RootModel) tea.Model {
return newModel, newModel.Init()
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Toggle Vanilla",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
currentModel.root.GetCurrentInstallation().Vanilla = !currentModel.root.GetCurrentInstallation().Vanilla
return currentModel, nil
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Profiles",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
Expand Down
Loading