Skip to content

Commit

Permalink
fix & rework install
Browse files Browse the repository at this point in the history
  • Loading branch information
maradotwebp committed Apr 17, 2022
1 parent 7eec773 commit 05dc9e1
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 98 deletions.
26 changes: 14 additions & 12 deletions src/modpack/install.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Users of Pax can choose between three different strategies which decide how the "best" version
## of a mod to install is selected.

import std/[options, sequtils]
import std/sequtils
import ../api/cfcore
import ../modpack/[loader, version]

Expand All @@ -15,6 +15,8 @@ type
## newest = newest version which is compatible with the minor modpack version.
Stable, Recommended, Newest

PaxInstallError* = object of IOError

converter toInstallStrategy*(str: string): InstallStrategy =
## Convert `str` to an InstallStrategy.
case str:
Expand All @@ -35,25 +37,25 @@ proc isNewest(file: CfAddonFile, modpackVersion: Version): bool =
## returns true if `file` is compatible according to InstallStrategy.newest.
return modpackVersion.minor in file.gameVersions.map(minor)

proc selectAddonFile*(files: seq[CfAddonFile], mpLoader: Loader, mpMcVersion: Version, strategy: InstallStrategy): Option[CfAddonFile] =
proc selectAddonFile*(files: seq[CfAddonFile], mpLoader: Loader, mpMcVersion: Version, strategy: InstallStrategy): CfAddonFile =
## Select the best mod file out of `files`, given the `manifest` and `strategy`.
var latestFile = none[CfAddonFile]()
for file in files:
if latestFile.isNone or latestFile.get().fileId < file.fileId:
if result.isNil or result.fileId < file.fileId:
let onFabric = mpLoader == Loader.Fabric and file.isFabricCompatible
let onForge = mpLoader == Loader.Forge and file.isForgeCompatible
let onStable = strategy == InstallStrategy.Stable and file.isStable(mpMcVersion)
let onRecommended = strategy == InstallStrategy.Recommended and file.isRecommended(mpMcVersion)
let onNewest = strategy == InstallStrategy.Newest and file.isNewest(mpMcVersion)
if onFabric or onForge:
if onStable or onRecommended or onNewest:
latestFile = some(file)
result = file

# In case nothing has been found, fallback to more generous install strategies
if latestFile.isNone:
return case strategy:
of InstallStrategy.Stable: selectAddonFile(files, mpLoader, mpMcVersion, InstallStrategy.Recommended)
of InstallStrategy.Recommended: selectAddonFile(files, mpLoader, mpMcVersion, InstallStrategy.Newest)
else: latestFile

return latestFile
if result.isNil:
case strategy:
of InstallStrategy.Stable:
return selectAddonFile(files, mpLoader, mpMcVersion, InstallStrategy.Recommended)
of InstallStrategy.Recommended:
return selectAddonFile(files, mpLoader, mpMcVersion, InstallStrategy.Newest)
else:
raise newException(PaxInstallError, "No suitable version found for file '" & files[0].name & "'.")
Loading

0 comments on commit 05dc9e1

Please sign in to comment.