Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Support multiple game versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sargunv committed Apr 14, 2020
1 parent 3b1a0fc commit 7fae86f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
14 changes: 7 additions & 7 deletions modsman-cli/src/main/kotlin/modsman/cli/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import com.beust.jcommander.JCommander
import com.beust.jcommander.Parameter
import com.beust.jcommander.ParameterException
import com.beust.jcommander.Parameters
import modsman.BuildConfig
import modsman.ModlistManager
import modsman.Modsman
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.runBlocking
import modsman.BuildConfig
import modsman.ModlistManager
import modsman.Modsman
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import kotlin.system.exitProcess
Expand Down Expand Up @@ -70,11 +70,11 @@ internal object RootCommand : CommandBase() {

@Parameters(commandNames = ["init"], commandDescription = "initialize a new mod list")
internal object InitCommand : CommandBase() {
@Parameter(names = ["--mc-version", "-M"], required = true, description = "the MC version this mods folder is for")
lateinit var gameVersion: String
@Parameter(names = ["--mc-version", "-M"], required = true, description = "the target MC version (repeatable)")
lateinit var gameVersions: List<String>

override suspend fun run(jc: JCommander): Int {
ModlistManager.init(Path.of(RootCommand.modsFolder), gameVersion).close()
ModlistManager.init(Path.of(RootCommand.modsFolder), gameVersions).close()
return 0
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ fun main(args: Array<String>) {
}

if (RootCommand.help) {
jc.parsedCommand?.let {jc.usage(it)} ?: jc.usage()
jc.parsedCommand?.let { jc.usage(it) } ?: jc.usage()
exitProcess(0)
}

Expand Down
4 changes: 2 additions & 2 deletions modsman-core/src/main/kotlin/modsman/exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ sealed class ModsmanException : RuntimeException {
constructor(message: String, cause: Throwable) : super(message, cause)
}

class ChooseFileException(version: String) :
ModsmanException("Failed to find a valid version matching '$version'")
class ChooseFileException(versions: List<String>) :
ModsmanException("Failed to find a valid version matching $versions")

class UpgradeException(mod: ModEntry, cause: ChooseFileException) :
ModsmanException("Failed to upgrade '${mod.projectName}', caused by: ${cause.message}", cause)
Expand Down
27 changes: 22 additions & 5 deletions modsman-core/src/main/kotlin/modsman/modlist.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package modsman

import com.google.gson.FieldNamingPolicy
import com.google.gson.GsonBuilder
import com.google.gson.*
import java.io.Closeable
import java.lang.reflect.Type
import java.nio.file.Files
import java.nio.file.Path

Expand All @@ -18,14 +18,31 @@ data class Modlist(
val mods: List<ModEntry>
) {
data class Config(
val gameVersion: String
val requiredGameVersions: List<String>
)

companion object {

const val fileName = ".modlist.json"

internal val gson = GsonBuilder()
.registerTypeAdapter(
Config::class.java,
JsonDeserializer { element: JsonElement,
_: Type,
_: JsonDeserializationContext ->
val obj = element.asJsonObject
Config(
when {
obj.has("required_game_versions") ->
obj.get("required_game_versions").asJsonArray.map { it.asString }
obj.has("game_version") ->
listOf(obj.get("game_version").asString)
else -> emptyList()
}
)
}
)
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setPrettyPrinting()
.create()!!
Expand Down Expand Up @@ -70,14 +87,14 @@ class ModlistManager(val modsPath: Path, modlist: Modlist) : Closeable {
override fun close() = save()

companion object {
fun init(modsPath: Path, gameVersion: String): ModlistManager {
fun init(modsPath: Path, gameVersions: List<String>): ModlistManager {
modsPath.resolve(Modlist.fileName).let {
if (Files.exists(it))
throw FileAlreadyExistsException(it.toFile())
else
return ModlistManager(
modsPath = modsPath,
modlist = Modlist(config = Modlist.Config(gameVersion), mods = arrayListOf())
modlist = Modlist(config = Modlist.Config(gameVersions), mods = arrayListOf())
)
}
}
Expand Down
9 changes: 5 additions & 4 deletions modsman-core/src/main/kotlin/modsman/modsman.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ class Modsman(
.filter { file ->
file.isAvailable &&
!file.isAlternate &&
file.gameVersion.any { version ->
modlist.config.gameVersion in version
modlist.config.requiredGameVersions.all { configVersion ->
file.gameVersion.any { fileVersion ->
configVersion in fileVersion
}
}
}
.maxBy { file -> file.fileDate }
?.let { it }
return ret ?: throw ChooseFileException(modlist.config.gameVersion)
return ret ?: throw ChooseFileException(modlist.config.requiredGameVersions)
}

private fun deleteFile(mod: ModEntry): Boolean {
Expand Down
9 changes: 6 additions & 3 deletions modsman-gui/src/main/kotlin/modsman/gui/MainApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ class MainApp : Application() {
return chooser.showDialog(stage)?.toPath() ?: exitProcess(0)
}

private fun chooseGameVersion(): String {
private fun chooseGameVersions(): List<String> {
val dialog = TextInputDialog("1.14")
dialog.headerText = "Mod list not found." +
"\nEnter a game version to initialize a new mod list." +
"\nModsman will match to any versions that contain this version as a substring." +
"\nFor example, the input \"1.14\" matches mods for \"1.14\", \"1.14.1\", and \"1.14-Snapshot\"."
dialog.title = title
return dialog.showAndWait().orElse(null) ?: exitProcess(0)
return dialog
.showAndWait()
.map { it.split("\\s+,\\s+") }
.orElseGet { exitProcess(0) }
}

private fun createModsman(stage: Stage): Modsman {
val path = chooseDirectory(stage)
if (!Files.exists(path.resolve(Modlist.fileName))) {
ModlistManager.init(path, chooseGameVersion()).close()
ModlistManager.init(path, chooseGameVersions()).close()
}
return Modsman(path, 10)
}
Expand Down
2 changes: 1 addition & 1 deletion modsman-gui/src/main/kotlin/modsman/gui/MainController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class MainController : Initializable {

modsman?.let {
path.text = "${it.modlist.modsPath.toAbsolutePath()}"
version.text = "MC ${it.modlist.config.gameVersion}"
version.text = it.modlist.config.requiredGameVersions.joinToString(", ")
tableView.items.addAll(it.modlist.mods)
}
}
Expand Down

0 comments on commit 7fae86f

Please sign in to comment.