diff --git a/modsman-cli/src/main/kotlin/modsman/cli/main.kt b/modsman-cli/src/main/kotlin/modsman/cli/main.kt index 6a60022..9a52587 100644 --- a/modsman-cli/src/main/kotlin/modsman/cli/main.kt +++ b/modsman-cli/src/main/kotlin/modsman/cli/main.kt @@ -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 @@ -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 + @Parameter( + names = ["--require-version", "-R"], + required = true, + description = "required game version tag; specify multiple times for multiple versions" + ) + lateinit var requiredGameVersions: List + + @Parameter( + names = ["--exclude-version", "-X"], + required = true, + description = "excluded game version tag; specify multiple times for multiple versions" + ) + lateinit var excludedGameVersions: List 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 } } diff --git a/modsman-core/src/main/kotlin/modsman/modlist.kt b/modsman-core/src/main/kotlin/modsman/modlist.kt index f49e7bd..59f9860 100644 --- a/modsman-core/src/main/kotlin/modsman/modlist.kt +++ b/modsman-core/src/main/kotlin/modsman/modlist.kt @@ -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 @@ -13,36 +13,18 @@ data class ModEntry( val fileName: String ) +data class ModlistConfig( + val requiredGameVersions: List, + val excludedGameVersions: List +) + data class Modlist( - val config: Config, + val config: ModlistConfig, val mods: List ) { - data class Config( - val requiredGameVersions: List - ) - 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()!! @@ -87,14 +69,14 @@ class ModlistManager(val modsPath: Path, modlist: Modlist) : Closeable { override fun close() = save() companion object { - fun init(modsPath: Path, gameVersions: List): 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()) ) } } diff --git a/modsman-core/src/main/kotlin/modsman/modsman.kt b/modsman-core/src/main/kotlin/modsman/modsman.kt index a209a7f..5f8eb77 100644 --- a/modsman-core/src/main/kotlin/modsman/modsman.kt +++ b/modsman-core/src/main/kotlin/modsman/modsman.kt @@ -15,7 +15,7 @@ import java.util.concurrent.Executors class Modsman( modsPath: Path, - val numConcurrent: Int + numConcurrent: Int ) : Closeable { val modlist = ModlistManager.load(modsPath) @@ -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 } @@ -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 { @@ -191,8 +196,6 @@ class Modsman( } } - fun save() = modlist.save() - override fun close() { modlist.close() downloadPool.shutdown()