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

Commit

Permalink
add excluded_game_versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sargunv committed Apr 16, 2020
1 parent 8e51163 commit bfe8733
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
24 changes: 21 additions & 3 deletions modsman-cli/src/main/kotlin/modsman/cli/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.runBlocking
import modsman.BuildConfig
import modsman.ModlistConfig
import modsman.ModlistManager
import modsman.Modsman
import java.nio.file.NoSuchFileException
Expand Down Expand Up @@ -70,11 +71,28 @@ 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 target MC version (repeatable)")
lateinit var gameVersions: List<String>
@Parameter(
names = ["--require-version", "-R"],
required = true,
description = "required game version tag; specify multiple times for multiple versions"
)
lateinit var requiredGameVersions: List<String>

@Parameter(
names = ["--exclude-version", "-X"],
required = true,
description = "excluded game version tag; specify multiple times for multiple versions"
)
lateinit var excludedGameVersions: List<String>

override suspend fun run(jc: JCommander): Int {
ModlistManager.init(Paths.get(RootCommand.modsFolder), gameVersions).close()
ModlistManager.init(
Paths.get(RootCommand.modsFolder),
ModlistConfig(
requiredGameVersions = requiredGameVersions,
excludedGameVersions = excludedGameVersions
)
).close()
return 0
}
}
Expand Down
38 changes: 10 additions & 28 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.*
import com.google.gson.FieldNamingPolicy
import com.google.gson.GsonBuilder
import java.io.Closeable
import java.lang.reflect.Type
import java.nio.file.Files
import java.nio.file.Path

Expand All @@ -13,36 +13,18 @@ data class ModEntry(
val fileName: String
)

data class ModlistConfig(
val requiredGameVersions: List<String>,
val excludedGameVersions: List<String>
)

data class Modlist(
val config: Config,
val config: ModlistConfig,
val mods: List<ModEntry>
) {
data class Config(
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 @@ -87,14 +69,14 @@ class ModlistManager(val modsPath: Path, modlist: Modlist) : Closeable {
override fun close() = save()

companion object {
fun init(modsPath: Path, gameVersions: List<String>): ModlistManager {
fun init(modsPath: Path, config: ModlistConfig): ModlistManager {
modsPath.resolve(Modlist.fileName).let {
if (Files.exists(it))
throw FileAlreadyExistsException(it.toFile())
else
return ModlistManager(
modsPath = modsPath,
modlist = Modlist(config = Modlist.Config(gameVersions), mods = arrayListOf())
modlist = Modlist(config = config, mods = arrayListOf())
)
}
}
Expand Down
11 changes: 7 additions & 4 deletions modsman-core/src/main/kotlin/modsman/modsman.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.util.concurrent.Executors

class Modsman(
modsPath: Path,
val numConcurrent: Int
numConcurrent: Int
) : Closeable {

val modlist = ModlistManager.load(modsPath)
Expand All @@ -33,6 +33,11 @@ class Modsman(
file.gameVersion.any { fileVersion ->
configVersion in fileVersion
}
} &&
modlist.config.excludedGameVersions.none { configVersion ->
file.gameVersion.any { fileVersion ->
configVersion in fileVersion
}
}
}
.maxBy { file -> file.fileDate }
Expand All @@ -52,7 +57,7 @@ class Modsman(
}

private fun readToBytes(jarPath: Path): ByteArray {
return Files.newInputStream(jarPath).use { it.readAllBytes() }
return Files.readAllBytes(jarPath)
}

private suspend fun fingerprint(jarPath: Path): Long {
Expand Down Expand Up @@ -191,8 +196,6 @@ class Modsman(
}
}

fun save() = modlist.save()

override fun close() {
modlist.close()
downloadPool.shutdown()
Expand Down

0 comments on commit bfe8733

Please sign in to comment.