2022-04-14 01:27:39 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-02 20:07:15 +00:00
|
|
|
"fmt"
|
2022-04-14 01:27:39 +00:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/Khan/genqlient/graphql"
|
|
|
|
"github.com/Masterminds/semver/v3"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/ficsit"
|
2022-05-02 20:07:15 +00:00
|
|
|
"github.com/spf13/viper"
|
2022-04-14 01:27:39 +00:00
|
|
|
)
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
const smlDownloadTemplate = `https://github.com/satisfactorymodding/SatisfactoryModLoader/releases/download/%s/SML.zip`
|
|
|
|
|
2022-04-14 01:27:39 +00:00
|
|
|
type DependencyResolver struct {
|
|
|
|
apiClient graphql.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDependencyResolver(apiClient graphql.Client) DependencyResolver {
|
|
|
|
return DependencyResolver{apiClient: apiClient}
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
func (d DependencyResolver) ResolveModDependencies(constraints map[string]string, lockFile *LockFile, gameVersion int) (LockFile, error) {
|
|
|
|
smlVersionsDB, err := ficsit.SMLVersions(context.TODO(), d.apiClient)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed fetching SMl versions")
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
copied := make(map[string][]string, len(constraints))
|
|
|
|
for k, v := range constraints {
|
|
|
|
copied[k] = []string{v}
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
instance := &resolvingInstance{
|
|
|
|
Resolver: d,
|
|
|
|
InputLock: lockFile,
|
2022-06-03 22:17:02 +00:00
|
|
|
ToResolve: copied,
|
2022-05-02 20:07:15 +00:00
|
|
|
OutputLock: make(LockFile),
|
|
|
|
SMLVersions: smlVersionsDB,
|
|
|
|
GameVersion: gameVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := instance.Step(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance.OutputLock, nil
|
|
|
|
}
|
2022-04-14 01:27:39 +00:00
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
type resolvingInstance struct {
|
|
|
|
Resolver DependencyResolver
|
|
|
|
|
|
|
|
InputLock *LockFile
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
ToResolve map[string][]string
|
2022-05-02 20:07:15 +00:00
|
|
|
|
|
|
|
OutputLock LockFile
|
|
|
|
|
|
|
|
SMLVersions *ficsit.SMLVersionsResponse
|
|
|
|
GameVersion int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolvingInstance) Step() error {
|
|
|
|
if len(r.ToResolve) > 0 {
|
|
|
|
if err := r.LockStep(make(map[string]bool)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-14 01:27:39 +00:00
|
|
|
|
|
|
|
converted := make([]ficsit.ModVersionConstraint, 0)
|
2022-05-02 20:07:15 +00:00
|
|
|
for id, constraint := range r.ToResolve {
|
|
|
|
if id != "SML" {
|
|
|
|
converted = append(converted, ficsit.ModVersionConstraint{
|
|
|
|
ModIdOrReference: id,
|
2022-06-03 22:17:02 +00:00
|
|
|
Version: constraint[0],
|
2022-05-02 20:07:15 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if existingSML, ok := r.OutputLock[id]; ok {
|
2022-06-03 22:17:02 +00:00
|
|
|
for _, cs := range constraint {
|
|
|
|
smlVersionConstraint, _ := semver.NewConstraint(cs)
|
|
|
|
if !smlVersionConstraint.Check(semver.MustParse(existingSML.Version)) {
|
|
|
|
return errors.Errorf("mod %s version %s does not match constraint %s",
|
|
|
|
id,
|
|
|
|
existingSML.Version,
|
|
|
|
constraint,
|
|
|
|
)
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var chosenSMLVersion *semver.Version
|
|
|
|
for _, version := range r.SMLVersions.SmlVersions.Sml_versions {
|
|
|
|
if version.Satisfactory_version > r.GameVersion {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
currentVersion := semver.MustParse(version.Version)
|
2022-06-03 22:17:02 +00:00
|
|
|
|
|
|
|
matches := true
|
|
|
|
for _, cs := range constraint {
|
|
|
|
smlVersionConstraint, _ := semver.NewConstraint(cs)
|
|
|
|
|
|
|
|
if !smlVersionConstraint.Check(currentVersion) {
|
|
|
|
matches = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches {
|
2022-05-02 20:07:15 +00:00
|
|
|
if chosenSMLVersion == nil || currentVersion.GreaterThan(chosenSMLVersion) {
|
|
|
|
chosenSMLVersion = currentVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if chosenSMLVersion == nil {
|
2022-06-03 22:17:02 +00:00
|
|
|
return errors.Errorf("could not find an SML version that matches constraint %s and game version %d", constraint, r.GameVersion)
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.OutputLock[id] = LockedMod{
|
|
|
|
Version: chosenSMLVersion.String(),
|
|
|
|
Link: fmt.Sprintf(smlDownloadTemplate, chosenSMLVersion.String()),
|
|
|
|
Dependencies: map[string]string{},
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 01:27:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
nextResolve := make(map[string][]string)
|
2022-04-14 01:27:39 +00:00
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
// TODO Cache
|
|
|
|
dependencies, err := ficsit.ResolveModDependencies(context.TODO(), r.Resolver.apiClient, converted)
|
2022-04-14 01:27:39 +00:00
|
|
|
if err != nil {
|
2022-05-02 20:07:15 +00:00
|
|
|
return errors.Wrap(err, "failed resolving mod dependencies")
|
2022-04-14 01:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, mod := range dependencies.Mods {
|
|
|
|
modVersions := make([]ModVersion, len(mod.Versions))
|
|
|
|
for i, version := range mod.Versions {
|
|
|
|
versionDependencies := make([]VersionDependency, len(version.Dependencies))
|
|
|
|
|
|
|
|
for j, dependency := range version.Dependencies {
|
|
|
|
versionDependencies[j] = VersionDependency{
|
|
|
|
ModReference: dependency.Mod_id,
|
|
|
|
Constraint: dependency.Condition,
|
|
|
|
Optional: dependency.Optional,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modVersions[i] = ModVersion{
|
|
|
|
ID: version.Id,
|
|
|
|
Version: version.Version,
|
2022-05-02 20:07:15 +00:00
|
|
|
Link: viper.GetString("api-base") + version.Link,
|
|
|
|
Hash: version.Hash,
|
2022-04-14 01:27:39 +00:00
|
|
|
Dependencies: versionDependencies,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(modVersions, func(i, j int) bool {
|
|
|
|
a := semver.MustParse(modVersions[i].Version)
|
|
|
|
b := semver.MustParse(modVersions[j].Version)
|
|
|
|
return b.LessThan(a)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Pick latest version
|
2022-05-02 20:07:15 +00:00
|
|
|
// TODO Clone and branch
|
2022-06-03 22:17:02 +00:00
|
|
|
var selectedVersion ModVersion
|
|
|
|
for _, version := range modVersions {
|
|
|
|
matches := true
|
|
|
|
|
|
|
|
for _, cs := range r.ToResolve[mod.Mod_reference] {
|
|
|
|
resolvingConstraint, _ := semver.NewConstraint(cs)
|
|
|
|
if !resolvingConstraint.Check(semver.MustParse(version.Version)) {
|
|
|
|
matches = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches {
|
|
|
|
selectedVersion = version
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if selectedVersion.Version == "" {
|
|
|
|
return errors.Errorf("no version of %s matches constraints", mod.Mod_reference)
|
|
|
|
}
|
2022-04-14 01:27:39 +00:00
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
if _, ok := r.OutputLock[mod.Mod_reference]; ok {
|
|
|
|
if r.OutputLock[mod.Mod_reference].Version != selectedVersion.Version {
|
|
|
|
return errors.New("failed resolving dependencies. requires different versions of " + mod.Mod_reference)
|
2022-04-14 01:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
modDependencies := make(map[string]string)
|
|
|
|
for _, dependency := range selectedVersion.Dependencies {
|
|
|
|
if !dependency.Optional {
|
|
|
|
modDependencies[dependency.ModReference] = dependency.Constraint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.OutputLock[mod.Mod_reference] = LockedMod{
|
|
|
|
Version: selectedVersion.Version,
|
|
|
|
Hash: selectedVersion.Hash,
|
|
|
|
Link: selectedVersion.Link,
|
|
|
|
Dependencies: modDependencies,
|
|
|
|
}
|
2022-04-14 01:27:39 +00:00
|
|
|
|
|
|
|
for _, dependency := range selectedVersion.Dependencies {
|
2022-05-02 20:07:15 +00:00
|
|
|
if previousSelectedVersion, ok := r.OutputLock[dependency.ModReference]; ok {
|
2022-04-14 01:27:39 +00:00
|
|
|
constraint, _ := semver.NewConstraint(dependency.Constraint)
|
|
|
|
if !constraint.Check(semver.MustParse(previousSelectedVersion.Version)) {
|
2022-05-02 20:07:15 +00:00
|
|
|
return errors.Errorf("mod %s version %s does not match constraint %s",
|
2022-04-14 01:27:39 +00:00
|
|
|
dependency.ModReference,
|
|
|
|
previousSelectedVersion.Version,
|
|
|
|
dependency.Constraint,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
if resolving, ok := nextResolve[dependency.ModReference]; ok {
|
2022-05-02 20:07:15 +00:00
|
|
|
constraint, _ := semver.NewConstraint(dependency.Constraint)
|
2022-06-03 22:17:02 +00:00
|
|
|
|
|
|
|
for _, cs := range resolving {
|
|
|
|
resolvingConstraint, _ := semver.NewConstraint(cs)
|
|
|
|
intersects, _ := constraint.Intersects(resolvingConstraint)
|
|
|
|
if !intersects {
|
|
|
|
return errors.Errorf("mod %s constraint %s does not intersect with %s",
|
|
|
|
dependency.ModReference,
|
|
|
|
resolving,
|
|
|
|
dependency.Constraint,
|
|
|
|
)
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dependency.Optional {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
nextResolve[dependency.ModReference] = append(nextResolve[dependency.ModReference], dependency.Constraint)
|
2022-04-14 01:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
r.ToResolve = nextResolve
|
|
|
|
|
2022-04-14 01:27:39 +00:00
|
|
|
for _, constraint := range converted {
|
2022-05-02 20:07:15 +00:00
|
|
|
if _, ok := r.OutputLock[constraint.ModIdOrReference]; !ok {
|
|
|
|
return errors.New("failed resolving dependency: " + constraint.ModIdOrReference)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.ToResolve) > 0 {
|
|
|
|
if err := r.Step(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolvingInstance) LockStep(viewed map[string]bool) error {
|
|
|
|
added := false
|
|
|
|
if r.InputLock != nil {
|
2022-06-03 22:17:02 +00:00
|
|
|
for modReference, constraints := range r.ToResolve {
|
2022-05-02 20:07:15 +00:00
|
|
|
if _, ok := viewed[modReference]; ok {
|
2022-04-14 01:27:39 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
viewed[modReference] = true
|
|
|
|
|
|
|
|
if locked, ok := (*r.InputLock)[modReference]; ok {
|
2022-06-03 22:17:02 +00:00
|
|
|
passes := true
|
|
|
|
|
|
|
|
for _, cs := range constraints {
|
|
|
|
constraint, _ := semver.NewConstraint(cs)
|
|
|
|
if !constraint.Check(semver.MustParse(locked.Version)) {
|
|
|
|
passes = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if passes {
|
2022-05-02 20:07:15 +00:00
|
|
|
delete(r.ToResolve, modReference)
|
|
|
|
r.OutputLock[modReference] = locked
|
|
|
|
for k, v := range locked.Dependencies {
|
|
|
|
if alreadyResolving, ok := r.ToResolve[k]; ok {
|
2022-06-03 22:17:02 +00:00
|
|
|
newConstraint, _ := semver.NewConstraint(v)
|
|
|
|
for _, resolvingConstraint := range alreadyResolving {
|
|
|
|
cs2, _ := semver.NewConstraint(resolvingConstraint)
|
|
|
|
intersects, _ := newConstraint.Intersects(cs2)
|
|
|
|
if !intersects {
|
|
|
|
return errors.Errorf("mod %s constraint %s does not intersect with %s",
|
|
|
|
k,
|
|
|
|
v,
|
|
|
|
alreadyResolving,
|
|
|
|
)
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
2022-06-03 22:17:02 +00:00
|
|
|
|
|
|
|
r.ToResolve[k] = append(r.ToResolve[k], v)
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if outVersion, ok := r.OutputLock[k]; ok {
|
|
|
|
constraint, _ := semver.NewConstraint(v)
|
|
|
|
if !constraint.Check(semver.MustParse(outVersion.Version)) {
|
|
|
|
return errors.Errorf("mod %s version %s does not match constraint %s",
|
|
|
|
k,
|
|
|
|
outVersion.Version,
|
|
|
|
v,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
r.ToResolve[k] = append(r.ToResolve[k], v)
|
2022-05-02 20:07:15 +00:00
|
|
|
added = true
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 01:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
if added {
|
|
|
|
if err := r.LockStep(viewed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-04-14 01:27:39 +00:00
|
|
|
}
|