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: store API responses in a local registry for offline mode #68

Merged
merged 4 commits into from
Oct 2, 2024
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
118 changes: 0 additions & 118 deletions cli/cache/integrity.go

This file was deleted.

81 changes: 45 additions & 36 deletions cli/cache/cache.go → cli/cache/mod_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,44 @@ import (
"path/filepath"
"strings"

"github.com/mircearoata/pubgrub-go/pubgrub/semver"
"github.com/puzpuzpuz/xsync/v3"
"github.com/spf13/viper"
)

const IconFilename = "Resources/Icon128.png" // This is the path UE expects for the icon

type File struct {
Icon *string
ModReference string
Hash string
Plugin UPlugin
Size int64
type Mod struct {
ModReference string
Name string
Author string
Icon *string
LatestVersion string
}

var loadedCache *xsync.MapOf[string, []File]
var loadedMods *xsync.MapOf[string, Mod]

func GetCache() (*xsync.MapOf[string, []File], error) {
if loadedCache != nil {
return loadedCache, nil
func GetCacheMods() (*xsync.MapOf[string, Mod], error) {
if loadedMods != nil {
return loadedMods, nil
}
return LoadCache()
return LoadCacheMods()
}

func GetCacheMod(mod string) ([]File, error) {
cache, err := GetCache()
func GetCacheMod(mod string) (Mod, error) {
cache, err := GetCacheMods()
if err != nil {
return nil, err
return Mod{}, err
}
value, _ := cache.Load(mod)
return value, nil
}

func LoadCache() (*xsync.MapOf[string, []File], error) {
loadedCache = xsync.NewMapOf[string, []File]()
func LoadCacheMods() (*xsync.MapOf[string, Mod], error) {
loadedMods = xsync.NewMapOf[string, Mod]()
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
if _, err := os.Stat(downloadCache); os.IsNotExist(err) {
return loadedCache, nil
return loadedMods, nil
}

items, err := os.ReadDir(downloadCache)
Expand All @@ -60,32 +61,45 @@ func LoadCache() (*xsync.MapOf[string, []File], error) {
if item.IsDir() {
continue
}
if item.Name() == integrityFilename {
continue
}

_, err = addFileToCache(item.Name())
if err != nil {
slog.Error("failed to add file to cache", slog.String("file", item.Name()), slog.Any("err", err))
}
}
return loadedCache, nil
return loadedMods, nil
}

func addFileToCache(filename string) (*File, error) {
func addFileToCache(filename string) (*Mod, error) {
cacheFile, err := readCacheFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read cache file: %w", err)
}

loadedCache.Compute(cacheFile.ModReference, func(oldValue []File, _ bool) ([]File, bool) {
return append(oldValue, *cacheFile), false
loadedMods.Compute(cacheFile.ModReference, func(oldValue Mod, loaded bool) (Mod, bool) {
if !loaded {
return *cacheFile, false
}
oldVersion, err := semver.NewVersion(oldValue.LatestVersion)
if err != nil {
slog.Error("failed to parse version", slog.String("version", oldValue.LatestVersion), slog.Any("err", err))
return *cacheFile, false
}
newVersion, err := semver.NewVersion(cacheFile.LatestVersion)
if err != nil {
slog.Error("failed to parse version", slog.String("version", cacheFile.LatestVersion), slog.Any("err", err))
return oldValue, false
}
if newVersion.Compare(oldVersion) > 0 {
return *cacheFile, false
}
return oldValue, false
})

return cacheFile, nil
}

func readCacheFile(filename string) (*File, error) {
func readCacheFile(filename string) (*Mod, error) {
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
path := filepath.Join(downloadCache, filename)
stat, err := os.Stat(path)
Expand Down Expand Up @@ -132,11 +146,6 @@ func readCacheFile(filename string) (*File, error) {

modReference := strings.TrimSuffix(upluginFile.Name, ".uplugin")

hash, err := getFileHash(filename)
if err != nil {
return nil, fmt.Errorf("failed to get file hash: %w", err)
}

var iconFile *zip.File
for _, file := range reader.File {
if file.Name == IconFilename {
Expand All @@ -160,11 +169,11 @@ func readCacheFile(filename string) (*File, error) {
icon = &iconData
}

return &File{
ModReference: modReference,
Hash: hash,
Size: size,
Icon: icon,
Plugin: uplugin,
return &Mod{
ModReference: modReference,
Name: uplugin.FriendlyName,
Author: uplugin.CreatedBy,
Icon: icon,
LatestVersion: uplugin.SemVersion,
}, nil
}
8 changes: 7 additions & 1 deletion cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/viper"

"github.com/satisfactorymodding/ficsit-cli/cli/cache"
"github.com/satisfactorymodding/ficsit-cli/cli/localregistry"
"github.com/satisfactorymodding/ficsit-cli/cli/provider"
"github.com/satisfactorymodding/ficsit-cli/ficsit"
)
Expand Down Expand Up @@ -45,11 +46,16 @@ func InitCLI(apiOnly bool) (*GlobalContext, error) {
return nil, fmt.Errorf("failed to initialize installations: %w", err)
}

_, err = cache.LoadCache()
_, err = cache.LoadCacheMods()
if err != nil {
return nil, fmt.Errorf("failed to load cache: %w", err)
}

err = localregistry.Init()
if err != nil {
return nil, fmt.Errorf("failed to initialize local registry: %w", err)
}

globalContext = &GlobalContext{
Installations: installations,
Profiles: profiles,
Expand Down
Loading
Loading