Skip to content

Commit

Permalink
Improve and fix archive data loading
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobkmar committed Jun 25, 2021
1 parent 79e0123 commit dd8fc78
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
13 changes: 7 additions & 6 deletions src/main/kotlin/net/axay/pacmc/commands/Install.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package net.axay.pacmc.commands
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.mordant.rendering.TextColors
import com.github.ajalt.mordant.rendering.TextColors.*
Expand All @@ -14,7 +13,6 @@ import io.ktor.util.cio.*
import io.ktor.utils.io.*
import kotlinx.coroutines.*
import kotlinx.dnq.query.eq
import kotlinx.dnq.query.first
import kotlinx.dnq.query.firstOrNull
import kotlinx.dnq.query.query
import net.axay.pacmc.data.MinecraftVersion
Expand All @@ -25,6 +23,7 @@ import net.axay.pacmc.requests.data.CurseProxyFile
import net.axay.pacmc.requests.data.CurseProxyProject
import net.axay.pacmc.requests.data.CurseProxyProjectInfo
import net.axay.pacmc.storage.Xodus
import net.axay.pacmc.storage.Xodus.xodus
import net.axay.pacmc.storage.data.PacmcFile
import net.axay.pacmc.storage.data.XdArchive
import net.axay.pacmc.storage.data.XdMod
Expand Down Expand Up @@ -60,7 +59,8 @@ object Install : CliktCommand(
private val mod by argument()

override fun run() = runBlocking(Dispatchers.Default) {
val (archivePath, minecraftVersion) = Xodus.getArchiveData(archiveName) ?: return@runBlocking
val archive = xodus { Xodus.getArchiveOrNull(archiveName) } ?: return@runBlocking
val (archivePath, minecraftVersion) = xodus { archive.path to archive.minecraftVersion }

var modId: String? = mod
var files: List<CurseProxyFile>? = null
Expand Down Expand Up @@ -130,7 +130,7 @@ object Install : CliktCommand(
terminal.println("Installing the mod at ${gray(archivePath)}")
terminal.println()

downloadFile(modId, file, archivePath, "curseforge", file.id.toString(), modInfo, true)
downloadFile(modId, file, archivePath, "curseforge", file.id.toString(), modInfo, true, archive)

val dependencies = dependenciesDeferred.await()
if (dependencies.isNotEmpty()) {
Expand All @@ -139,7 +139,7 @@ object Install : CliktCommand(
terminal.println()

dependencies.forEach {
downloadFile(it.addonId, it.file, archivePath, "curseforge", it.file.id.toString(), it.info, false)
downloadFile(it.addonId, it.file, archivePath, "curseforge", it.file.id.toString(), it.info, false, archive)
}
}

Expand Down Expand Up @@ -204,12 +204,13 @@ object Install : CliktCommand(
versionId: String,
modInfo: Deferred<CurseProxyProjectInfo>?,
persistent: Boolean,
archive: XdArchive,
) = coroutineScope {
// download the mod file to the given archive (and display progress)
terminal.println("Downloading " + brightCyan(file.fileName))

Xodus.ioTransaction {
val archiveMods = XdArchive.query(XdArchive::name eq archiveName).first().mods
val archiveMods = archive.mods
val archiveMod = archiveMods.query(XdMod::id eq modId).firstOrNull()
if (archiveMod != null) {
if (persistent) archiveMod.persistent = true
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/net/axay/pacmc/commands/Update.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.dnq.query.filter
import kotlinx.dnq.query.first
import kotlinx.dnq.query.toList
import net.axay.pacmc.commands.Install.findBestFile
import net.axay.pacmc.requests.CurseProxy
import net.axay.pacmc.requests.data.CurseProxyFile
import net.axay.pacmc.storage.Xodus
import net.axay.pacmc.storage.Xodus.xodus
import net.axay.pacmc.storage.data.PacmcFile
import net.axay.pacmc.storage.data.XdArchive
import net.axay.pacmc.storage.data.XdMod
import net.axay.pacmc.terminal
import java.io.File
Expand All @@ -35,7 +33,8 @@ object Update : CliktCommand(
private class UpdateMod(val xdMod: XdMod, val id: String, val name: String)

override fun run() = runBlocking(Dispatchers.Default) {
val (archivePath, minecraftVersion) = Xodus.getArchiveData(archiveName) ?: return@runBlocking
val archive = xodus { Xodus.getArchiveOrNull(archiveName) } ?: return@runBlocking
val (archivePath, minecraftVersion) = xodus { archive.path to archive.minecraftVersion }

terminal.println("Checking for updates for the mods at ${gray(archivePath)}")
terminal.println()
Expand All @@ -44,7 +43,7 @@ object Update : CliktCommand(
val updateCounter = AtomicInteger(0)
val unsureCounter = AtomicInteger(0)

val allMods = xodus { XdArchive.filter { it.name eq archiveName }.first().mods }
val allMods = xodus { archive.mods }

val mods = xodus {
allMods.filter { it.persistent eq true }.toList().map { UpdateMod(it, it.id, it.name) }
Expand Down Expand Up @@ -113,7 +112,7 @@ object Update : CliktCommand(
terminal.println()

updateMods.forEach {
Install.downloadFile(it.first.id, it.second, archivePath, "curseforge", it.second.id.toString(), null, true)
Install.downloadFile(it.first.id, it.second, archivePath, "curseforge", it.second.id.toString(), null, true, archive)
updateCounter.incrementAndGet()
}
}
Expand All @@ -124,7 +123,7 @@ object Update : CliktCommand(
terminal.println()

freshDependencies.forEach {
Install.downloadFile(it.addonId, it.file, archivePath, "curseforge", it.file.id.toString(), it.info, false)
Install.downloadFile(it.addonId, it.file, archivePath, "curseforge", it.file.id.toString(), it.info, false, archive)
updateCounter.incrementAndGet()
}
}
Expand Down
15 changes: 0 additions & 15 deletions src/main/kotlin/net/axay/pacmc/storage/Xodus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,6 @@ object Xodus {
}
}

/**
* Loads archive data from the database.
*
* @return a pair, where the first element is the path, the second one is
* the minecraft version
*/
fun getArchiveData(name: String) = store.transactional {
val archive = getArchiveOrNull(name)
if (archive != null) {
archive.path to archive.minecraftVersion
} else {
null
}
}

fun getArchiveOrNull(name: String): XdArchive? {
val archive = XdArchive.query(XdArchive::name eq name).firstOrNull()
if (archive == null)
Expand Down

0 comments on commit dd8fc78

Please sign in to comment.