Skip to content

Commit

Permalink
add ability to convert addons/addon files to json
Browse files Browse the repository at this point in the history
  • Loading branch information
froehlichA committed Apr 19, 2022
1 parent 974778a commit 956aec6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/api/cfcore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ converter addonFilesFromForgeSvc*(json: JsonNode): seq[CfAddonFile] =
## creates addon files from json retrieved by an forgesvc endpoint
return json.getElems().map(addonFileFromForgeSvc)

converter toJson*(file: CfAddonFile): JsonNode =
## creates json from an addon file
result = %* {
"id": file.fileId,
"displayName": file.name,
"releaseType": file.releaseType.ord,
"downloadUrl": file.downloadUrl,
"gameVersions": file.gameVersions.map((x) => $x),
"dependencies": file.dependencies.map((x) => %* {
"relationType": RequiredDependencyType,
"modId": x
})
}

converter addonFromForgeSvc*(json: JsonNode): CfAddon =
## creates an addon from json retrieved by an forgesvc endpoint
result = CfAddon()
Expand All @@ -83,6 +97,26 @@ converter addonsFromForgeSvc*(json: JsonNode): seq[CfAddon] =
## creates addons from json retrieved by an forgesvc endpoint
result = json.getElems().map(addonFromForgeSvc)

converter toJson*(addon: CfAddon): JsonNode =
result = %* {
"id": addon.projectId,
"name": addon.name,
"summary": addon.description,
"links": {
"websiteUrl": addon.websiteUrl
},
"authors": addon.authors.map((x) => %* {
"name": x
}),
"downloadCount": addon.downloads,
"gamePopularityRank": addon.popularity,
"latestFiles": addon.latestFiles.map((x) => x.toJson()),
"latestFilesIndexes": addon.gameVersionLatestFiles.map((x) => %* {
"gameVersion": x.version.`$`,
"fileId": x.fileId
})
}

proc isFabricCompatible*(file: CfAddonFile): bool =
## returns true if `file` is compatible with the fabric loader.
if "Fabric".Version in file.gameVersions:
Expand Down
33 changes: 33 additions & 0 deletions tests/api/tcfcore.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import std/[json, sequtils, sugar]
import api/cfcore
import modpack/version

proc initCfAddonFile(fileId: int, name: string, gameVersions: seq[string], releaseType: CfAddonFileReleaseType): CfAddonFile =
result = CfAddonFile()
result.fileId = fileId
result.name = name
result.releaseType = releaseType
result.downloadUrl = "https://download-here.com/" & name
result.gameVersions = gameVersions.map((x) => x.Version)

proc initCfAddon(projectId: int, name: string, gameVersionLatestFiles: seq[tuple[version: Version, fileId: int]]): CfAddon =
result = CfAddon()
result.projectId = projectId
result.name = name
result.description = "description"
result.websiteUrl = "https://website-url.com/" & name
result.authors = @["user1", "user2"]
result.downloads = 102039
result.popularity = 0.5
result.latestFiles = @[]
result.gameVersionLatestFiles = gameVersionLatestFiles

block: # AddonFile from/to JSON
let addonFile = initCfAddonFile(300, "jei-1.0.2.jar", @["1.16.1", "1.16.2", "Forge"], CfAddonFileReleaseType.Beta)

doAssert addonFile.toJson.addonFileFromForgeSvc.toJson == addonFile.toJson

block: # Addon from/to JSON
let addon = initCfAddon(200, "Just Enough Items (JEI)", @[(version: "1.16".Version, fileId: 2)])

doAssert addon.toJson.addonFromForgeSvc.toJson == addon.toJson

0 comments on commit 956aec6

Please sign in to comment.