Skip to content

Commit

Permalink
Allow selecting any loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jul 2, 2024
1 parent f0cc61b commit 3b9b396
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
10 changes: 8 additions & 2 deletions src/main/kotlin/io/github/gaming32/additiveinstaller/Loader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package io.github.gaming32.additiveinstaller
import io.github.z4kn4fein.semver.Version
import io.github.z4kn4fein.semver.toVersion

enum class Loader(val dependencyName: String, val apiRoot: String, val addMods: (Version) -> Pair<String, String>) {
enum class Loader(
val dependencyName: String,
val apiRoot: String,
val addMods: (Version) -> Pair<String, String>
) {
FABRIC("fabric-loader", "https://meta.fabricmc.net/v2", { Pair("fabric.addMods", "") }),
QUILT(
"quilt-loader", "https://meta.quiltmc.org/v3",
Expand All @@ -13,5 +17,7 @@ enum class Loader(val dependencyName: String, val apiRoot: String, val addMods:
if (it >= "0.18.1-beta.13".toVersion()) "/*" else ""
)
}
)
);

override fun toString() = name.lowercase()
}
32 changes: 15 additions & 17 deletions src/main/kotlin/io/github/gaming32/additiveinstaller/Modpack.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package io.github.gaming32.additiveinstaller

import com.google.gson.JsonElement
import java.util.*
import javax.imageio.ImageIO

private const val PROJECT_BASE = "https://api.modrinth.com/v2/project"

class Modpack(val id: String) {
val versions = requestCriticalJson("$PROJECT_BASE/$id/version").asJsonArray
.asSequence()
.map(JsonElement::getAsJsonObject)
.map { PackVersion(this, it) }
.run {
val result = mutableMapOf<String, MutableMap<String, PackVersion>>()
for (version in this) {
val game = result.getOrPut(version.gameVersion, ::mutableMapOf)
game[version.packVersion] = maxOf(
game[version.packVersion],
version,
Comparator.comparingInt { it?.loader?.ordinal ?: -1 }
)!!
val versions: Map<String, Map<String, Map<Loader, PackVersion>>> =
requestCriticalJson("$PROJECT_BASE/$id/version").asJsonArray
.asSequence()
.map(JsonElement::getAsJsonObject)
.map { PackVersion(this, it) }
.run {
val result = mutableMapOf<String, MutableMap<String, MutableMap<Loader, PackVersion>>>()
for (version in this) {
val byPackVersion = result.getOrPut(version.gameVersion, ::mutableMapOf)
val byLoader = byPackVersion.getOrPut(version.packVersion) { EnumMap(Loader::class.java) }
byLoader[version.loader] = version
}
result
}
@Suppress("USELESS_CAST") // It's a cast to an immutable interface, Kotlin. That's not unnecessary (imo).
result as Map<String, Map<String, PackVersion>>
}

val name = id.capitalize()
val windowTitle = I18N.getString("window.title", name)
Expand All @@ -33,7 +31,7 @@ class Modpack(val id: String) {
?.prefix("data:image/png;base64,")
val supportedMcVersions = buildSet {
for ((key, value) in versions) {
if (value.values.any { it.data["featured"].asBoolean }) {
if (value.values.asSequence().flatMap { it.values }.any(PackVersion::isSupported)) {
add(key)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class PackVersion(val modpack: Modpack, val data: JsonObject) {
}.uppercase().let(Loader::valueOf)
}

val launcherFolderPath = "${modpack.id}/$packVersion-$gameVersion"
val launcherVersionId = "${modpack.id}-$packVersion-$gameVersion"
val launcherProfileId = "${modpack.id}-$gameVersion"
val launcherFolderPath = "${modpack.id}/$packVersion-$gameVersion-$loader"
val launcherVersionId = "${modpack.id}-$packVersion-$gameVersion-$loader"
val launcherProfileId = "${modpack.id}-$gameVersion-$loader"
val isSupported = data["featured"].asBoolean

fun install(destination: Path, progressHandler: ProgressHandler) =
PackInstaller(this, destination, progressHandler)
Expand Down
39 changes: 29 additions & 10 deletions src/main/kotlin/io/github/gaming32/additiveinstaller/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ fun main() {

val iconLabel = JLabel(ImageIcon(selectedPack.image))

val minecraftVersion = JComboBox<String>()
val includeUnsupportedMinecraft = JCheckBox(I18N.getString("include.unsupported.minecraft"))
val packVersion = JComboBox<String>()
val loaderSelector = JComboBox<String>()

lateinit var setupMinecraftVersions: () -> Unit

val minecraftVersion = JComboBox<String>().apply {
minecraftVersion.apply {
addItemListener {
val gameVersion = selectedItem as? String ?: return@addItemListener
packVersion.removeAllItems()
Expand All @@ -66,16 +67,25 @@ fun main() {
?.forEach(packVersion::addItem)
selectedPack.versions[gameVersion]
?.entries
?.first { it.value.data["featured"].asBoolean }
?.first { it.value.values.any(PackVersion::isSupported) }
?.let { packVersion.selectedItem = it.key }
}
}

val includeUnsupportedMinecraft = JCheckBox(I18N.getString("include.unsupported.minecraft")).apply {
addActionListener { setupMinecraftVersions() }
packVersion.addItemListener {
val gameVersion = minecraftVersion.selectedItem as? String ?: return@addItemListener
val selectedVersion = packVersion.selectedItem as? String ?: return@addItemListener
val loaders = selectedPack.versions[gameVersion]
?.get(selectedVersion) ?: return@addItemListener
loaderSelector.removeAllItems()
for ((loader, version) in loaders) {
if (version.isSupported || includeUnsupportedMinecraft.isSelected) {
loaderSelector.addItem(loader.name.lowercase().capitalize())
}
}
}

setupMinecraftVersions = {
fun setupMinecraftVersions() {
val mcVersion = minecraftVersion.selectedItem
minecraftVersion.removeAllItems()
val all = includeUnsupportedMinecraft.isSelected
Expand All @@ -91,6 +101,8 @@ fun main() {
}
setupMinecraftVersions()

includeUnsupportedMinecraft.addActionListener { setupMinecraftVersions() }

val includeFeatures = JCheckBox(I18N.getString("include.non.performance.features")).apply {
isSelected = true
addActionListener {
Expand Down Expand Up @@ -122,8 +134,9 @@ fun main() {
val install = JButton(I18N.getString("install")).apply {
addActionListener {
enableOptions(false)
val selectedMcVersion = minecraftVersion.selectedItem
val selectedPackVersion = packVersion.selectedItem
val selectedMcVersion = minecraftVersion.selectedItem as String
val selectedPackVersion = packVersion.selectedItem as String
val selectedLoader = Loader.valueOf((loaderSelector.selectedItem as String).uppercase())
val destinationPath = Path(installationDir.text)
if (!destinationPath.isDirectory()) {
if (destinationPath.exists()) {
Expand All @@ -132,6 +145,8 @@ fun main() {
I18N.getString("installation.dir.not.directory"),
title, JOptionPane.INFORMATION_MESSAGE
)
enableOptions(true)
return@addActionListener
} else {
destinationPath.createDirectories()
}
Expand All @@ -140,9 +155,10 @@ fun main() {
val error = try {
selectedPack.versions[selectedMcVersion]
?.get(selectedPackVersion)
?.get(selectedLoader)
?.install(destinationPath, JProgressBarProgressHandler(installProgress))
?: throw IllegalStateException(
"Couldn't find pack version $selectedPackVersion for $selectedMcVersion"
"Couldn't find pack version $selectedPackVersion for $selectedMcVersion with loader $selectedLoader"
)
null
} catch (t: Throwable) {
Expand Down Expand Up @@ -174,6 +190,7 @@ fun main() {
minecraftVersion.isEnabled = it
includeUnsupportedMinecraft.isEnabled = it
packVersion.isEnabled = it
loaderSelector.isEnabled = it
installationDir.isEnabled = it
browseButton.isEnabled = it
install.isEnabled = it
Expand All @@ -195,6 +212,8 @@ fun main() {
add(includeUnsupportedMinecraft.withLabel())
add(Box.createVerticalStrut(15))
add(packVersion.withLabel(I18N.getString("pack.version")))
add(Box.createVerticalStrut(5))
add(loaderSelector.withLabel(I18N.getString("mod.loader")))
add(Box.createVerticalStrut(15))
add(JPanel().apply {
layout = BoxLayout(this, BoxLayout.LINE_AXIS)
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/i18n/lang.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
modrinth.access.failed=Failed to access Modrinth. Please check your internet connection.
done=Done!
select.installation.folder=Select installation folder
include.unsupported.minecraft=Include unsupported Minecraft versions
include.unsupported.minecraft=Include unsupported Minecraft versions/loaders
include.non.performance.features=Include non-performance features
browse=Browse...
install=Install!
Expand All @@ -10,6 +10,7 @@ installation.success=Installation success!
installation.failed=Installation failed.
minecraft.version=Minecraft version:
pack.version=Pack version:
mod.loader=Mod loader:
install.to=Install to:
window.title={0} Installer
creating.version.folder=Creating version folder
Expand Down

0 comments on commit 3b9b396

Please sign in to comment.