From 05dc9e1ebd738d44288eb35b2e13e990ea7b4af8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Fr=C3=B6hlich?= Date: Sat, 16 Apr 2022 17:38:11 +0200 Subject: [PATCH] fix & rework install --- src/modpack/install.nim | 26 ++--- tests/modpack/tinstall.nim | 197 +++++++++++++++++++++---------------- 2 files changed, 125 insertions(+), 98 deletions(-) diff --git a/src/modpack/install.nim b/src/modpack/install.nim index 9914975..fb0a898 100644 --- a/src/modpack/install.nim +++ b/src/modpack/install.nim @@ -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] @@ -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: @@ -35,11 +37,10 @@ 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) @@ -47,13 +48,14 @@ proc selectAddonFile*(files: seq[CfAddonFile], mpLoader: Loader, mpMcVersion: Ve 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 \ No newline at end of file + 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 & "'.") \ No newline at end of file diff --git a/tests/modpack/tinstall.nim b/tests/modpack/tinstall.nim index 843b3ad..e62552b 100644 --- a/tests/modpack/tinstall.nim +++ b/tests/modpack/tinstall.nim @@ -1,5 +1,6 @@ -import options, sequtils, sugar -import api/cfcore, modpack/install, modpack/loader, modpack/manifest, modpack/version +import std/[sequtils, sugar] +import api/cfcore +import modpack/[install, loader, manifest, version] block: # InstallStrategy doAssert "stable".toInstallStrategy == InstallStrategy.Stable @@ -26,24 +27,27 @@ block: # select out of specified forge mods ] mcVersion = "1.12".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).isNone + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) mcVersion = "1.16".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.16.1".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.16.2".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] block: # select out of implied forge mods let loader = Loader.Forge @@ -55,24 +59,27 @@ block: # select out of implied forge mods ] mcVersion = "1.12".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).isNone + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) mcVersion = "1.16".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[2] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[2] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.16.1".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.16.2".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] block: # select out of specified fabric mods let loader = Loader.Fabric @@ -84,24 +91,27 @@ block: # select out of specified fabric mods ] mcVersion = "1.12".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).isNone + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) mcVersion = "1.14".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.14.1".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.14.4".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] block: # select out of implied fabric mods let loader = Loader.Fabric @@ -113,24 +123,27 @@ block: # select out of implied fabric mods ] mcVersion = "1.12".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).isNone + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) mcVersion = "1.14".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[2] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[2] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.14.1".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.14.4".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] block: # select out of mixed mods var loader: Loader @@ -150,64 +163,76 @@ block: # select out of mixed mods loader = Loader.Forge mcVersion = "1.12".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).isNone + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) mcVersion = "1.14".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[7] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[7] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[6] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[7] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[7] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[6] mcVersion = "1.14.1".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[7] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[7] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[6] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[7] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[7] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[6] mcVersion = "1.16".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[4] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[2] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[4] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[2] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[1] mcVersion = "1.16.1".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[1] mcVersion = "1.16.2".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[1] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[1] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[1] # Set loader to forge loader = Loader.Fabric mcVersion = "1.12".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).isNone + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) mcVersion = "1.14".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).isNone + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) mcVersion = "1.14.1".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).isNone - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).isNone + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) + doAssertRaises(PaxInstallError): + discard mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) mcVersion = "1.16".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[5] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[5] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[5] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[5] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.16.1".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] mcVersion = "1.16.2".Version - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended).get() == mods[0] - doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest).get() == mods[0] \ No newline at end of file + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Stable) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Recommended) == mods[0] + doAssert mods.selectAddonFile(loader, mcVersion, InstallStrategy.Newest) == mods[0] \ No newline at end of file