2023-12-06 04:47:41 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-16 14:19:53 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-12-06 04:47:41 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
resolver "github.com/satisfactorymodding/ficsit-resolver"
|
2023-12-06 04:47:41 +00:00
|
|
|
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/cli/cache"
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/ficsit"
|
|
|
|
)
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
type LocalProvider struct{}
|
2023-12-06 04:47:41 +00:00
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func NewLocalProvider() LocalProvider {
|
|
|
|
return LocalProvider{}
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func (p LocalProvider) Mods(_ context.Context, filter ficsit.ModFilter) (*ficsit.ModsResponse, error) {
|
2023-12-06 04:47:41 +00:00
|
|
|
cachedMods, err := cache.GetCache()
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to get cache: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mods := make([]ficsit.ModsModsGetModsModsMod, 0)
|
|
|
|
|
2023-12-07 16:57:31 +00:00
|
|
|
cachedMods.Range(func(modReference string, files []cache.File) bool {
|
2023-12-06 04:47:41 +00:00
|
|
|
if modReference == "SML" {
|
2023-12-07 16:57:31 +00:00
|
|
|
return true
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(filter.References) > 0 {
|
|
|
|
skip := true
|
|
|
|
|
|
|
|
for _, a := range filter.References {
|
|
|
|
if a == modReference {
|
|
|
|
skip = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if skip {
|
2023-12-07 16:57:31 +00:00
|
|
|
return true
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mods = append(mods, ficsit.ModsModsGetModsModsMod{
|
|
|
|
Id: modReference,
|
|
|
|
Name: files[0].Plugin.FriendlyName,
|
|
|
|
Mod_reference: modReference,
|
|
|
|
Last_version_date: time.Now(),
|
|
|
|
Created_at: time.Now(),
|
|
|
|
Downloads: 0,
|
|
|
|
Popularity: 0,
|
|
|
|
Hotness: 0,
|
|
|
|
})
|
2023-12-07 16:57:31 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
2023-12-06 04:47:41 +00:00
|
|
|
|
|
|
|
if filter.Limit == 0 {
|
|
|
|
filter.Limit = 25
|
|
|
|
}
|
|
|
|
|
|
|
|
low := filter.Offset
|
|
|
|
high := filter.Offset + filter.Limit
|
|
|
|
|
|
|
|
if low > len(mods) {
|
|
|
|
return &ficsit.ModsResponse{
|
|
|
|
Mods: ficsit.ModsModsGetMods{
|
|
|
|
Count: 0,
|
|
|
|
Mods: []ficsit.ModsModsGetModsModsMod{},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if high > len(mods) {
|
|
|
|
high = len(mods)
|
|
|
|
}
|
|
|
|
|
|
|
|
mods = mods[low:high]
|
|
|
|
|
|
|
|
return &ficsit.ModsResponse{
|
|
|
|
Mods: ficsit.ModsModsGetMods{
|
|
|
|
Count: len(mods),
|
|
|
|
Mods: mods,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func (p LocalProvider) GetMod(_ context.Context, modReference string) (*ficsit.GetModResponse, error) {
|
2023-12-06 04:47:41 +00:00
|
|
|
cachedModFiles, err := cache.GetCacheMod(modReference)
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to get cache: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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, ",") {
|
|
|
|
authors = append(authors, ficsit.GetModModAuthorsUserMod{
|
|
|
|
Role: "Unknown",
|
|
|
|
User: ficsit.GetModModAuthorsUserModUser{
|
|
|
|
Username: author,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ficsit.GetModResponse{
|
|
|
|
Mod: ficsit.GetModMod{
|
|
|
|
Id: modReference,
|
|
|
|
Name: cachedModFiles[0].Plugin.FriendlyName,
|
|
|
|
Mod_reference: modReference,
|
|
|
|
Created_at: time.Now(),
|
|
|
|
Views: 0,
|
|
|
|
Downloads: 0,
|
|
|
|
Authors: authors,
|
|
|
|
Full_description: "",
|
|
|
|
Source_url: "",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func (p LocalProvider) SMLVersions(_ context.Context) ([]resolver.SMLVersion, error) {
|
2023-12-06 04:47:41 +00:00
|
|
|
cachedSMLFiles, err := cache.GetCacheMod("SML")
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to get cache: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
smlVersions := make([]resolver.SMLVersion, 0)
|
2023-12-06 04:47:41 +00:00
|
|
|
|
|
|
|
for _, smlFile := range cachedSMLFiles {
|
2023-12-16 11:59:58 +00:00
|
|
|
smlVersions = append(smlVersions, resolver.SMLVersion{
|
|
|
|
ID: "SML:" + smlFile.Plugin.SemVersion,
|
|
|
|
Version: smlFile.Plugin.SemVersion,
|
|
|
|
SatisfactoryVersion: 0, // TODO: where can this be obtained from?
|
2023-12-06 04:47:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
return smlVersions, nil
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func (p LocalProvider) ModVersionsWithDependencies(_ context.Context, modID string) ([]resolver.ModVersion, error) {
|
2023-12-06 04:47:41 +00:00
|
|
|
cachedModFiles, err := cache.GetCacheMod(modID)
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to get cache: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
versions := make([]resolver.ModVersion, 0)
|
2023-12-06 04:47:41 +00:00
|
|
|
|
|
|
|
for _, modFile := range cachedModFiles {
|
2023-12-16 11:59:58 +00:00
|
|
|
versions = append(versions, resolver.ModVersion{
|
2023-12-13 23:34:01 +00:00
|
|
|
ID: modID + ":" + modFile.Plugin.SemVersion,
|
2023-12-06 04:47:41 +00:00
|
|
|
Version: modFile.Plugin.SemVersion,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
return versions, nil
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func (p LocalProvider) GetModName(_ context.Context, modReference string) (*resolver.ModName, error) {
|
2023-12-06 04:47:41 +00:00
|
|
|
cachedModFiles, err := cache.GetCacheMod(modReference)
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to get cache: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(cachedModFiles) == 0 {
|
|
|
|
return nil, errors.New("mod not found")
|
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
return &resolver.ModName{
|
|
|
|
ID: modReference,
|
|
|
|
Name: cachedModFiles[0].Plugin.FriendlyName,
|
|
|
|
ModReference: modReference,
|
2023-12-06 04:47:41 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2023-12-16 11:59:58 +00:00
|
|
|
|
|
|
|
func (p LocalProvider) IsOffline() bool {
|
|
|
|
return true
|
|
|
|
}
|