-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into main
- Loading branch information
Showing
74 changed files
with
746 additions
and
665 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,58 @@ | ||
## Provides `getModLoaderId` which retrieves the modloader id (a string specifying | ||
## the type & version of the modloader) given a minecraft version and modloader. | ||
## the type & version of the modloader) given a minecraft version and type of modloader. | ||
## | ||
## Pax currently has support for the Forge & Fabric modloader. The information about | ||
## what loader version corresponds to what minecraft version is retrieved online | ||
## and should be generally up-to-date 99% of the time. | ||
|
||
import asyncdispatch, json, options, strutils, sugar | ||
import std/[asyncdispatch, json, strutils] | ||
import http | ||
import ../modpack/version, ../modpack/loader | ||
|
||
const | ||
## base url of the fabric metadata endpoint | ||
fabricBaseUrl = "https://meta.fabricmc.net/v2/versions/loader/" | ||
## base url of the curse metadata api endpoint | ||
forgeBaseUrl = "http://raw.githubusercontent.com/MultiMC/meta-upstream/master/forge/derived_index.json" | ||
forgeBaseUrl = "https://cfproxy.fly.dev/v1/minecraft/modloader" | ||
|
||
proc getFabricLoaderVersion(mcVersion: Version): Future[Option[string]] {.async.} = | ||
type | ||
MetadataClientError* = object of HttpRequestError | ||
|
||
proc getFabricLoaderVersion(mcVersion: Version): Future[string] {.async.} = | ||
## get the fabric loader version fitting for the given minecraft version | ||
let url = fabricBaseUrl & $mcVersion | ||
var json: JsonNode | ||
try: | ||
json = get(url.Url).await.parseJson | ||
let json: JsonNode = try: | ||
get(url.Url).await.parseJson | ||
except HttpRequestError: | ||
return none[string]() | ||
raise newException(MetadataClientError, "'" & $mcVersion & "' is not a valid mc version.") | ||
let loaderElems = json.getElems() | ||
if loaderElems.len == 0: | ||
return none[string]() | ||
raise newException(MetadataClientError, "'" & $mcVersion & "' is not a valid mc version.") | ||
let ver = loaderElems[0]["loader"]["version"].getStr() | ||
return some(ver) | ||
return ver | ||
|
||
proc getForgeLoaderVersion(mcVersion: Version, latest: bool): Future[Option[string]] {.async.} = | ||
proc getForgeLoaderVersion(mcVersion: Version, latest: bool): Future[string] {.async.} = | ||
## get the forge loader version fitting for the given minecraft version | ||
let json = get(forgeBaseUrl.Url).await.parseJson | ||
let recommendedVersion = json{"by_mcversion", $mcVersion, "recommended"}.getStr() | ||
let latestVersion = json{"by_mcversion", $mcVersion, "latest"}.getStr() | ||
let forgeVersion = if latest: | ||
latestVersion | ||
else: | ||
if $recommendedVersion != "": recommendedVersion else: latestVersion | ||
return if forgeVersion != "": some(forgeVersion) else: none[string]() | ||
let url = forgeBaseUrl & "?version=" & $mcVersion | ||
let json: JsonNode = try: | ||
get(url.Url).await.parseJson | ||
except HttpRequestError: | ||
raise newException(MetadataClientError, "'" & $mcVersion & "' is not a valid mc version.") | ||
let searchKey = if latest: "latest" else: "recommended" | ||
for item in json["data"].items(): | ||
if item[searchKey].getBool(): | ||
return item["name"].getStr() | ||
raise newException(MetadataClientError, "'" & $mcVersion & "' is not a valid mc version.") | ||
|
||
proc toModloaderId(loaderVersion: string, loader: Loader): string = | ||
## get the modloader id fitting for the given loader version and loader | ||
return case loader: | ||
of Loader.Forge: "forge-" & loaderVersion.split("-")[1] | ||
of Loader.Fabric: "fabric-" & loaderVersion | ||
|
||
proc getModloaderId*(mcVersion: Version, loader: Loader, latest: bool = false): Future[Option[string]] {.async.} = | ||
proc getModloaderId*(mcVersion: Version, loader: Loader, latest: bool = false): Future[string] {.async.} = | ||
## get the modloader id fitting for the given minecraft version and loader | ||
let loaderVersion = case loader: | ||
of Loader.Forge: await mcVersion.getForgeLoaderVersion(latest) | ||
of Loader.Fabric: await mcVersion.getFabricLoaderVersion() | ||
return loaderVersion.map((x) => toModloaderId(x, loader)) | ||
of Loader.Fabric: mcVersion.getFabricLoaderVersion().await.toModloaderId(loader) | ||
return loaderVersion |
Oops, something went wrong.