-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.go
152 lines (127 loc) · 4.28 KB
/
source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package resolver
import (
"context"
"fmt"
"maps"
"slices"
"github.com/mircearoata/pubgrub-go/pubgrub"
"github.com/mircearoata/pubgrub-go/pubgrub/helpers"
"github.com/mircearoata/pubgrub-go/pubgrub/semver"
"github.com/puzpuzpuz/xsync/v3"
)
type ficsitAPISource struct {
provider Provider
lockfile *LockFile
toInstall map[string]semver.Constraint
requiredTargets map[TargetName]bool
modVersionInfo *xsync.MapOf[string, []ModVersion]
gameVersion semver.Version
}
var clientTargets = map[TargetName]bool{
TargetNameWindows: true,
}
var serverTargets = map[TargetName]bool{
TargetNameWindowsServer: true,
TargetNameLinuxServer: true,
}
func (f *ficsitAPISource) GetPackageVersions(pkg string) ([]pubgrub.PackageVersion, error) {
// If root package, return the base list of dependencies
if pkg == rootPkg {
return []pubgrub.PackageVersion{{Version: semver.Version{}, Dependencies: f.toInstall}}, nil
}
// Ignore game dependency
if pkg == factoryGamePkg {
return []pubgrub.PackageVersion{{Version: f.gameVersion}}, nil
}
response, err := f.provider.ModVersionsWithDependencies(context.TODO(), pkg)
if err != nil {
return nil, fmt.Errorf("failed to fetch mod %s: %w", pkg, err)
}
f.modVersionInfo.Store(pkg, response)
versions := make([]pubgrub.PackageVersion, 0)
for _, modVersion := range response {
v, err := semver.NewVersion(modVersion.Version)
if err != nil {
return nil, fmt.Errorf("failed to parse version %s: %w", modVersion.Version, err)
}
matches, err := f.matchesTargetRequirements(modVersion)
if err != nil {
return nil, err
}
if !matches {
continue
}
dependencies := make(map[string]semver.Constraint)
optionalDependencies := make(map[string]semver.Constraint)
for _, dependency := range modVersion.Dependencies {
c, err := semver.NewConstraint(dependency.Condition)
if err != nil {
return nil, fmt.Errorf("failed to parse constraint %s: %w", dependency.Condition, err)
}
if dependency.Optional {
optionalDependencies[dependency.ModID] = c
} else {
dependencies[dependency.ModID] = c
}
}
// If a version range string is empty, no version will satisfy it
if modVersion.GameVersion != "" {
factoryGameConstraint, err := semver.NewConstraint(modVersion.GameVersion)
if err != nil {
return nil, fmt.Errorf("failed to parse game version constraint %s: %w", modVersion.GameVersion, err)
}
dependencies[factoryGamePkg] = factoryGameConstraint
}
versions = append(versions, pubgrub.PackageVersion{
Version: v,
Dependencies: dependencies,
OptionalDependencies: optionalDependencies,
})
}
return versions, nil
}
func (f *ficsitAPISource) PickVersion(pkg string, versions []semver.Version) semver.Version {
if f.lockfile != nil {
if existing, ok := f.lockfile.Mods[pkg]; ok {
v, err := semver.NewVersion(existing.Version)
if err == nil {
if slices.ContainsFunc(versions, func(version semver.Version) bool {
return v.Compare(version) == 0
}) {
return v
}
}
}
}
return helpers.StandardVersionPriority(versions)
}
func (f *ficsitAPISource) matchesTargetRequirements(modVersion ModVersion) (bool, error) {
if len(f.requiredTargets) == 0 {
return true, nil
}
requiredClientTargets := make(map[TargetName]bool)
requiredServerTargets := make(map[TargetName]bool)
for target := range f.requiredTargets {
if clientTargets[target] {
requiredClientTargets[target] = true
} else if serverTargets[target] {
requiredServerTargets[target] = true
} else {
return false, fmt.Errorf("unknown requested target %s", target)
}
}
missingClientTargets := maps.Clone(requiredClientTargets)
missingServerTargets := maps.Clone(requiredServerTargets)
for _, target := range modVersion.Targets {
delete(missingClientTargets, target.TargetName)
delete(missingServerTargets, target.TargetName)
}
if modVersion.RequiredOnRemote {
// All targets must be present
return len(missingClientTargets) == 0 && len(missingServerTargets) == 0, nil
}
// Don't consider as having all targets when no targets of that type were requested
hasAllClient := len(requiredClientTargets) > 0 && len(missingClientTargets) == 0
hasAllServer := len(requiredServerTargets) > 0 && len(missingServerTargets) == 0
return hasAllClient || hasAllServer, nil
}