Skip to content

Commit 154a300

Browse files
committed
consolidate code duplication, fix imports, some other small stuff
1 parent ecdebea commit 154a300

1 file changed

Lines changed: 40 additions & 68 deletions

File tree

src/main/kotlin/com/pistacium/modcheck/ModCheck.kt

Lines changed: 40 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import java.awt.Toolkit
66
import java.awt.datatransfer.StringSelection
77
import java.io.*
88
import java.net.URI
9+
import java.nio.file.*
910
import java.util.concurrent.*
1011
import javax.swing.JOptionPane
12+
import kotlin.io.path.extension
1113
import kotlin.system.exitProcess
1214

1315
object ModCheck {
@@ -95,7 +97,6 @@ object ModCheck {
9597
val mods = ModCheckUtils.json.decodeFromString<Meta>(
9698
URI.create("https://raw.githubusercontent.com/tildejustin/mcsr-meta/${if (applicationVersion == "dev") "staging" else "schema-6"}/mods.json").toURL().readText()
9799
).mods
98-
availableMods.clear()
99100
availableMods.addAll(mods)
100101

101102
// Defaults
@@ -118,7 +119,7 @@ object ModCheck {
118119
printHelp()
119120
return
120121
}
121-
"version", "-v", -> {
122+
"version", "-v" -> {
122123
println("ModCheck version: $applicationVersion")
123124
return
124125
}
@@ -193,38 +194,32 @@ object ModCheck {
193194
}
194195

195196
// Print out the values
196-
// Determine modsDir for debug print
197-
val basePath = java.nio.file.Paths.get(path)
198-
val mcPath = basePath.resolve("minecraft")
199-
val dotMcPath = basePath.resolve(".minecraft")
200-
val modsDirDebug = when {
201-
java.nio.file.Files.isDirectory(mcPath) && java.nio.file.Files.isDirectory(mcPath.resolve("mods")) -> mcPath.resolve("mods")
202-
java.nio.file.Files.isDirectory(dotMcPath) && java.nio.file.Files.isDirectory(dotMcPath.resolve("mods")) -> dotMcPath.resolve("mods")
203-
java.nio.file.Files.isDirectory(basePath.resolve("mods")) -> basePath.resolve("mods")
204-
else -> null
205-
}
206-
println("Options:")
207-
println(" Category: ${if (category == "rsg") "Random Seed Glitchless" else "Set Seed Glitchless"}")
208-
println(" OS: ${os.replaceFirstChar { it.uppercase() }}")
209-
println(" Accessibility: $accessibility")
210-
println(" Version: $version")
211-
println(" Instance Path: $path")
197+
val basePath = Paths.get(path)
198+
val mcPath = basePath.resolve("minecraft")
199+
val dotMcPath = basePath.resolve(".minecraft")
200+
val modsDir = when {
201+
Files.isDirectory(mcPath) && Files.isDirectory(mcPath.resolve("mods")) -> mcPath.resolve("mods")
202+
Files.isDirectory(dotMcPath) && Files.isDirectory(dotMcPath.resolve("mods")) -> dotMcPath.resolve("mods")
203+
Files.isDirectory(basePath.resolve("mods")) -> basePath.resolve("mods")
204+
basePath.endsWith("mods") -> basePath
205+
else -> null
206+
}
207+
208+
if (modsDir == null || !Files.exists(modsDir)) {
209+
println("No mods directory found at: $modsDir")
210+
return
211+
}
212+
213+
println("Options:")
214+
println(" Category: ${if (category == "rsg") "Random Seed Glitchless" else "Set Seed Glitchless"}")
215+
println(" OS: ${os.replaceFirstChar { it.uppercase() }}")
216+
println(" Accessibility: $accessibility")
217+
println(" Version: $version")
218+
println(" Mod Folder: $modsDir")
212219

213220
if (function == "download") {
214221
// 1. Select mods
215-
val basePath = java.nio.file.Paths.get(path)
216-
val mcPath = basePath.resolve("minecraft")
217-
val dotMcPath = basePath.resolve(".minecraft")
218-
val modsDir = when {
219-
java.nio.file.Files.isDirectory(mcPath) && java.nio.file.Files.isDirectory(mcPath.resolve("mods")) -> mcPath.resolve("mods")
220-
java.nio.file.Files.isDirectory(dotMcPath) && java.nio.file.Files.isDirectory(dotMcPath.resolve("mods")) -> dotMcPath.resolve("mods")
221-
java.nio.file.Files.isDirectory(basePath.resolve("mods")) -> basePath.resolve("mods")
222-
else -> null
223-
}
224-
if (modsDir == null) {
225-
println("No mods directory found at: $modsDir")
226-
return
227-
}
222+
// assumes that there are no conflicting recommended mods, which is a choice in meta design I will try to stick to
228223
val selectedMods = availableMods.filter { mod ->
229224
val modVersion = mod.getModVersion(version)
230225
if (modVersion == null) return@filter false
@@ -236,6 +231,7 @@ object ModCheck {
236231
?.versions?.any { version in it.target_version } == true
237232
) return@filter false
238233
if (mod.obsolete || modVersion.obsolete) return@filter false
234+
if (!mod.recommended) return@filter false
239235
for (trait in mod.traits) {
240236
if (trait == "ssg-only" && category != "ssg") return@filter false
241237
if (trait == "rsg-only" && category != "rsg") return@filter false
@@ -247,25 +243,20 @@ object ModCheck {
247243
if (selectedMods.isEmpty()) {
248244
println("Warning: No mods matched the selection criteria. Nothing to download.")
249245
return
250-
} else {
251-
for (mod in selectedMods) {
252-
println("Selected ${mod.name}")
253-
}
246+
}
247+
for (mod in selectedMods) {
248+
println("Selected ${mod.name}")
254249
}
255250
// 2. Download selected mods
256251
var count = 0
257252
for (mod in selectedMods) {
258-
val modVersion = mod.getModVersion(version)
259-
if (modVersion == null) {
260-
println("Skipping ${mod.name}: no version for $version")
261-
continue
262-
}
253+
val modVersion = mod.getModVersion(version)!! // null results are thrown out in initial filter
263254
val url = modVersion.url
264255
val filename = url.substringAfterLast("/")
265256
try {
266257
println("Downloading ${mod.name}")
267-
val bytes = java.net.URI.create(url).toURL().readBytes()
268-
java.nio.file.Files.write(modsDir.resolve(filename), bytes)
258+
val bytes = URI.create(url).toURL().readBytes()
259+
Files.write(modsDir.resolve(filename), bytes)
269260
} catch (e: Exception) {
270261
println("Failed to download ${mod.name}: ${e.message}")
271262
}
@@ -274,31 +265,12 @@ object ModCheck {
274265
println("Downloading mods complete")
275266
} else if (function == "update") {
276267
// 1. Find installed mods
277-
val basePath = java.nio.file.Paths.get(path)
278-
val mcPath = basePath.resolve("minecraft")
279-
val dotMcPath = basePath.resolve(".minecraft")
280-
val modsDir = when {
281-
java.nio.file.Files.isDirectory(mcPath) && java.nio.file.Files.isDirectory(mcPath.resolve("mods")) -> mcPath.resolve("mods")
282-
java.nio.file.Files.isDirectory(dotMcPath) && java.nio.file.Files.isDirectory(dotMcPath.resolve("mods")) -> dotMcPath.resolve("mods")
283-
java.nio.file.Files.isDirectory(basePath.resolve("mods")) -> basePath.resolve("mods")
284-
else -> null
285-
}
286-
if (modsDir == null || !java.nio.file.Files.exists(modsDir)) {
287-
println("No mods directory found at: $modsDir")
288-
return
289-
}
290-
println("Resolved mods directory: ${modsDir.toAbsolutePath()}")
291-
val modFiles = java.nio.file.Files.list(modsDir).use { it.iterator().asSequence().toList() }
292-
val toUpdate = mutableListOf<Triple<java.nio.file.Path, Meta.Mod, Meta.ModVersion>>()
268+
val modFiles = Files.list(modsDir)
269+
val toUpdate = mutableListOf<Triple<Path, Meta.Mod, Meta.ModVersion>>()
293270
for (file in modFiles) {
294-
if (!file.fileName.toString().endsWith(".jar")) continue
295-
val fmj = try { com.pistacium.modcheck.util.ModCheckUtils.readFabricModJson(file) } catch (_: Exception) { null }
271+
if (file.extension != "jar") continue
272+
val fmj = try { ModCheckUtils.readFabricModJson(file) } catch (_: Exception) { null }
296273
if (fmj == null) continue
297-
if (fmj.id == "serversiderng") {
298-
println("Deleting illegal mod: SSRNG (${file.fileName})")
299-
java.nio.file.Files.deleteIfExists(file)
300-
continue
301-
}
302274
val mod = availableMods.find { it.modid == fmj.id || it.name == fmj.name }
303275
if (mod != null) {
304276
val modVersion = mod.getModVersion(version)
@@ -317,9 +289,9 @@ object ModCheck {
317289
val filename = url.substringAfterLast("/")
318290
try {
319291
println("Updating ${mod.name} from ${oldFile.fileName} to $filename")
320-
val bytes = java.net.URI.create(url).toURL().readBytes()
321-
java.nio.file.Files.write(modsDir.resolve(filename), bytes)
322-
java.nio.file.Files.deleteIfExists(oldFile)
292+
val bytes = URI.create(url).toURL().readBytes()
293+
Files.write(modsDir.resolve(filename), bytes)
294+
Files.deleteIfExists(oldFile)
323295
} catch (e: Exception) {
324296
println("Failed to update ${mod.name}: ${e.message}")
325297
}

0 commit comments

Comments
 (0)