Skip to content

Commit

Permalink
chore: remove .integrity file, cache only latest mod details
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata committed Sep 10, 2024
1 parent b214dde commit 382278f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 173 deletions.
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
}
2 changes: 1 addition & 1 deletion cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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)
}
Expand Down
25 changes: 8 additions & 17 deletions cli/provider/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand All @@ -21,14 +20,14 @@ func NewLocalProvider() LocalProvider {
}

func (p LocalProvider) Mods(_ context.Context, filter ficsit.ModFilter) (*ficsit.ModsResponse, error) {
cachedMods, err := cache.GetCache()
cachedMods, err := cache.GetCacheMods()
if err != nil {
return nil, fmt.Errorf("failed to get cache: %w", err)
}

mods := make([]ficsit.ModsModsGetModsModsMod, 0)

cachedMods.Range(func(modReference string, files []cache.File) bool {
cachedMods.Range(func(modReference string, cachedMod cache.Mod) bool {
if len(filter.References) > 0 {
skip := true

Expand All @@ -46,7 +45,7 @@ func (p LocalProvider) Mods(_ context.Context, filter ficsit.ModFilter) (*ficsit

mods = append(mods, ficsit.ModsModsGetModsModsMod{
Id: modReference,
Name: files[0].Plugin.FriendlyName,
Name: cachedMod.Name,
Mod_reference: modReference,
Last_version_date: time.Now(),
Created_at: time.Now(),
Expand Down Expand Up @@ -89,18 +88,14 @@ func (p LocalProvider) Mods(_ context.Context, filter ficsit.ModFilter) (*ficsit
}

func (p LocalProvider) GetMod(_ context.Context, modReference string) (*ficsit.GetModResponse, error) {
cachedModFiles, err := cache.GetCacheMod(modReference)
cachedMod, err := cache.GetCacheMod(modReference)
if err != nil {
return nil, fmt.Errorf("failed to get cache: %w", err)
}

if len(cachedModFiles) == 0 {
return nil, errors.New("mod not found")
}

authors := make([]ficsit.GetModModAuthorsUserMod, 0)

for _, author := range strings.Split(cachedModFiles[0].Plugin.CreatedBy, ",") {
for _, author := range strings.Split(cachedMod.Author, ",") {
authors = append(authors, ficsit.GetModModAuthorsUserMod{
Role: "Unknown",
User: ficsit.GetModModAuthorsUserModUser{
Expand All @@ -112,7 +107,7 @@ func (p LocalProvider) GetMod(_ context.Context, modReference string) (*ficsit.G
return &ficsit.GetModResponse{
Mod: ficsit.GetModMod{
Id: modReference,
Name: cachedModFiles[0].Plugin.FriendlyName,
Name: cachedMod.Name,
Mod_reference: modReference,
Created_at: time.Now(),
Views: 0,
Expand All @@ -136,18 +131,14 @@ func (p LocalProvider) ModVersionsWithDependencies(_ context.Context, modID stri
}

func (p LocalProvider) GetModName(_ context.Context, modReference string) (*resolver.ModName, error) {
cachedModFiles, err := cache.GetCacheMod(modReference)
cachedMod, err := cache.GetCacheMod(modReference)
if err != nil {
return nil, fmt.Errorf("failed to get cache: %w", err)
}

if len(cachedModFiles) == 0 {
return nil, errors.New("mod not found")
}

return &resolver.ModName{
ID: modReference,
Name: cachedModFiles[0].Plugin.FriendlyName,
Name: cachedMod.Name,
ModReference: modReference,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/jackc/puddle/v2 v2.2.1
github.com/jlaffaye/ftp v0.2.0
github.com/lmittmann/tint v1.0.3
github.com/mircearoata/pubgrub-go v0.3.3
github.com/muesli/reflow v0.3.0
github.com/pkg/sftp v1.13.6
github.com/pterm/pterm v0.12.71
Expand Down Expand Up @@ -72,7 +73,6 @@ require (
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
github.com/mircearoata/pubgrub-go v0.3.3 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
Expand Down

0 comments on commit 382278f

Please sign in to comment.