Skip to content

Commit

Permalink
Add cache dir setting
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata committed Dec 30, 2023
1 parent 7fa4b9d commit c830405
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 4 deletions.
25 changes: 25 additions & 0 deletions backend/bindings/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,31 @@ func (a *App) OpenFileDialog(options OpenDialogOptions) (string, error) {
return file, nil
}

func (a *App) OpenDirectoryDialog(options OpenDialogOptions) (string, error) {
wailsFilters := make([]wailsRuntime.FileFilter, len(options.Filters))
for i, filter := range options.Filters {
wailsFilters[i] = wailsRuntime.FileFilter{
DisplayName: filter.DisplayName,
Pattern: filter.Pattern,
}
}
wailsOptions := wailsRuntime.OpenDialogOptions{
DefaultDirectory: options.DefaultDirectory,
DefaultFilename: options.DefaultFilename,
Title: options.Title,
Filters: wailsFilters,
ShowHiddenFiles: options.ShowHiddenFiles,
CanCreateDirectories: options.CanCreateDirectories,
ResolvesAliases: options.ResolvesAliases,
TreatPackagesAsDirectories: options.TreatPackagesAsDirectories,
}
file, err := wailsRuntime.OpenDirectoryDialog(a.ctx, wailsOptions)
if err != nil {
return "", errors.Wrap(err, "failed to open directory dialog")
}
return file, nil
}

func (a *App) ExternalInstallMod(modID, version string) {
wailsRuntime.EventsEmit(a.ctx, "externalInstallMod", modID, version)
}
Expand Down
80 changes: 80 additions & 0 deletions backend/bindings/ficsitcli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package ficsitcli
import (
"context"
"log/slog"
"os"
"time"

"github.com/mitchellh/go-ps"
"github.com/pkg/errors"
"github.com/satisfactorymodding/ficsit-cli/cli"
"github.com/satisfactorymodding/ficsit-cli/cli/provider"
"github.com/spf13/viper"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"

"github.com/satisfactorymodding/SatisfactoryModManager/backend/settings"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/utils"
)

type FicsitCLI struct {
Expand Down Expand Up @@ -76,3 +79,80 @@ func (f *FicsitCLI) setProgress(p *Progress) {
f.progress = p
wailsRuntime.EventsEmit(f.ctx, "progress", p)
}

func ValidateCacheDir(dir string) error {
stat, err := os.Stat(dir)
if err != nil {
if !os.IsNotExist(err) {
return errors.Wrapf(err, "failed to stat %s", dir)
}
} else {
if !stat.IsDir() {
return errors.Errorf("%s is not a directory", dir)
}
}
return nil
}

func MoveCacheDir(newDir string) error {
if newDir == viper.GetString("cache-dir") {
return nil
}

err := ValidateCacheDir(newDir)
if err != nil {
return err
}

err = os.MkdirAll(newDir, 0o755)
if err != nil {
if !os.IsExist(err) {
return errors.Wrapf(err, "failed to create %s", newDir)
}
}

items, err := os.ReadDir(newDir)
if err != nil {
return errors.Wrapf(err, "failed to check if directory %s is empty", newDir)
}
if len(items) > 0 {
return errors.Errorf("directory %s is not empty", newDir)
}

oldCacheDir := viper.GetString("cache-dir")
// Move contents of oldCacheDir to dir
if oldCacheDir != "" && oldCacheDir != newDir {
err := moveCacheData(oldCacheDir, newDir)
if err != nil {
return err
}
}

viper.Set("cache-dir", newDir)
return nil
}

func moveCacheData(oldCacheDir, newDir string) error {
oldStat, err := os.Stat(oldCacheDir)
if err != nil {
if os.IsNotExist(err) {
// Nothing to move
return nil
}
return errors.Wrapf(err, "failed to stat %s", oldCacheDir)
}
if !oldStat.IsDir() {
return errors.Errorf("%s is not a directory", oldCacheDir)
}

// Perform the move atomically
copySuccess, err := utils.MoveRecursive(oldCacheDir, newDir)
if err != nil {
if !copySuccess {
return err
}
slog.Error("failed to move cache dir", slog.Any("error", err))
}

return nil
}
22 changes: 22 additions & 0 deletions backend/bindings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package bindings

import (
"context"
"log/slog"

"github.com/spf13/viper"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"

"github.com/satisfactorymodding/SatisfactoryModManager/backend/bindings/ficsitcli"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/settings"
)

Expand Down Expand Up @@ -175,3 +178,22 @@ func (s *Settings) SetAnnouncementViewed(announcement string) {
_ = settings.SaveSettings()
wailsRuntime.EventsEmit(s.ctx, "viewedAnnouncements", settings.Settings.ViewedAnnouncements)
}

func (s *Settings) SetCacheDir(dir string) error {
if dir == "" {
dir = viper.GetString("default-cache-dir")
}
err := ficsitcli.MoveCacheDir(dir)
if err != nil {
slog.Error("failed to set cache dir", slog.Any("error", err))
return err
}
settings.Settings.CacheDir = dir
_ = settings.SaveSettings()
wailsRuntime.EventsEmit(s.ctx, "cacheDir", dir)
return nil
}

func (s *Settings) GetCacheDir() string {
return viper.GetString("cache-dir")
}
2 changes: 2 additions & 0 deletions backend/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type settings struct {

Konami bool `json:"konami"`
LaunchButton string `json:"launchButton"`

CacheDir string `json:"cacheDir,omitempty"`
}

var Settings = settings{
Expand Down
86 changes: 86 additions & 0 deletions backend/utils/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package utils

import (
"io"
"os"
"path/filepath"

"github.com/pkg/errors"
)

func IsIn(dir, path string) bool {
rel, err := filepath.Rel(dir, path)
if err != nil {
return false
}
return filepath.IsLocal(rel)
}

func CopyRecursive(from, to string) error {
return filepath.Walk(from, func(path string, info os.FileInfo, err error) error { //nolint:wrapcheck
if err != nil {
return err
}
if IsIn(to, path) {
return nil
}
relPath, err := filepath.Rel(from, path)
if err != nil {
return err //nolint:wrapcheck
}
newPath := filepath.Join(to, relPath)
if info.IsDir() {
err := os.Mkdir(newPath, 0o755)
if err != nil && !os.IsExist(err) {
return err //nolint:wrapcheck
}
return nil
}
f, err := os.Open(path)
if err != nil {
return err //nolint:wrapcheck
}
defer f.Close()
f2, err := os.Create(newPath)
if err != nil {
return err //nolint:wrapcheck
}
defer f2.Close()
_, err = io.Copy(f2, f)
return err //nolint:wrapcheck
})
}

func MoveRecursive(from, to string) (bool, error) {
err := CopyRecursive(from, to)
if err != nil {
return false, errors.Wrapf(err, "failed to copy %s to %s", from, to)
}
err = filepath.Walk(from, func(path string, info os.FileInfo, err error) error {
if err != nil {
if !os.IsNotExist(err) {
return err
}
return nil
}
if IsIn(path, to) {
// Skip parent directories of destination
return nil
}
if IsIn(to, path) {
// Skip contents of destination
return nil
}
err = os.RemoveAll(path)
if err != nil {
if !os.IsNotExist(err) {
return err //nolint:wrapcheck
}
}
return nil
})
if err != nil {
return true, errors.Wrapf(err, "failed to remove %s", from)
}
return true, nil
}
Loading

0 comments on commit c830405

Please sign in to comment.