Prefer game versions according to acceptable versions list (fixes #181)

The acceptable versions list should now be specified in order of preference, where the last version is the most preferable Minecraft version
This commit is contained in:
comp500
2023-02-14 16:10:06 +00:00
parent d667447a88
commit d38d279d98
9 changed files with 123 additions and 145 deletions

View File

@@ -10,9 +10,9 @@ type Updater interface {
// ParseUpdate takes an unparsed interface{} (as a map[string]interface{}), and returns an Updater for a mod file.
// This can be done using the mapstructure library or your own parsing methods.
ParseUpdate(map[string]interface{}) (interface{}, error)
// CheckUpdate checks whether there is an update for each of the mods in the given slice, for the given MC version,
// CheckUpdate checks whether there is an update for each of the mods in the given slice,
// called for all of the mods that this updater handles
CheckUpdate([]Mod, string, Pack) ([]UpdateCheck, error)
CheckUpdate([]Mod, Pack) ([]UpdateCheck, error)
// DoUpdate carries out the update previously queried in CheckUpdate, on each Mod's metadata,
// given pointers to Mods and the value of CachedState for each mod
DoUpdate([]*Mod, []interface{}) error

View File

@@ -164,6 +164,16 @@ func (pack Pack) GetMCVersion() (string, error) {
return mcVersion, nil
}
// GetSupportedMCVersions gets the versions of Minecraft this pack allows in downloaded mods, ordered by preference (highest = most desirable)
func (pack Pack) GetSupportedMCVersions() ([]string, error) {
mcVersion, ok := pack.Versions["minecraft"]
if !ok {
return nil, errors.New("no minecraft version specified in modpack")
}
allVersions := append(append([]string(nil), viper.GetStringSlice("acceptable-game-versions")...), mcVersion)
return allVersions, nil
}
func (pack Pack) GetPackName() string {
if pack.Name == "" {
return "export"

View File

@@ -145,3 +145,16 @@ func ComponentToFriendlyName(component string) string {
return component
}
}
// HighestSliceIndex returns the highest index of the given values in the slice (-1 if no value is found in the slice)
func HighestSliceIndex(slice []string, values []string) int {
highest := -1
for _, val := range values {
for i, v := range slice {
if v == val && i > highest {
highest = i
}
}
}
return highest
}