Skip to content

Commit c6cacce

Browse files
committed
Minor cleanup
1 parent ec461ad commit c6cacce

File tree

5 files changed

+116
-124
lines changed

5 files changed

+116
-124
lines changed

src/main/kotlin/com/lambda/loader/BaseMavenVersionController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ abstract class BaseMavenVersionController(
5858
val versionNodes = document.getElementsByTagName("version")
5959
val versions = mutableListOf<String>()
6060

61-
for (i in 0 until versionNodes.length) {
61+
(0 until versionNodes.length).forEach { i ->
6262
val version = versionNodes.item(i).textContent
6363
versions.add(version)
6464
}

src/main/kotlin/com/lambda/loader/Cache.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Cache(baseDir: File = File("lambda")) {
1616
if (!versionCacheDir.exists()) versionCacheDir.mkdirs()
1717
}
1818

19-
2019
fun checkSumBytes(bytes: ByteArray): String {
2120
val digest = md.digest(bytes)
2221
return digest.joinToString("") { "%02x".format(it) }
@@ -26,14 +25,15 @@ class Cache(baseDir: File = File("lambda")) {
2625
val baseName = currentFileName.substringBeforeLast("-").substringBeforeLast(".")
2726

2827
versionCacheDir.listFiles()?.forEach { file ->
29-
if (file.isFile && file.name.endsWith(".jar") && file.name != currentFileName) {
30-
val fileBaseName = file.name.substringBeforeLast("-").substringBeforeLast(".")
31-
if (fileBaseName == baseName) {
32-
val deleted = file.delete()
33-
if (ConfigManager.config.debug)
34-
if (deleted) logger.info("Deleted old version: ${file.name}")
35-
else logger.warning("Failed to delete old version: ${file.name}")
36-
}
28+
if (!file.isFile || !file.name.endsWith(".jar") || file.name == currentFileName) return@forEach
29+
30+
val fileBaseName = file.name.substringBeforeLast("-").substringBeforeLast(".")
31+
if (fileBaseName != baseName) return@forEach
32+
33+
val deleted = file.delete()
34+
if (ConfigManager.config.debug) {
35+
if (deleted) logger.info("Deleted old version: ${file.name}")
36+
else logger.warning("Failed to delete old version: ${file.name}")
3737
}
3838
}
3939
}

src/main/kotlin/com/lambda/loader/LambdaLoaderInitializer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class LoaderInitializer : PreLaunchEntrypoint {
7575
FabricUtil.injectAccessWidener(accessWidenerPath)
7676
}
7777

78-
for (mixinFile in FabricUtil.getMixinFileNames(latestVersion)) {
78+
FabricUtil.getMixinFileNames(latestVersion).forEach { mixinFile ->
7979
Mixins.addConfiguration(mixinFile)
8080
}
8181

src/main/kotlin/com/lambda/loader/config/ConfigManager.kt

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,28 @@ object ConfigManager {
1111

1212
var config: Config = loadConfig()
1313

14-
private fun loadConfig(): Config {
15-
return if (configFile.exists()) {
16-
try {
17-
val json = configFile.readText()
18-
val rootObject = gson.fromJson(json, JsonObject::class.java)
19-
20-
val autoUpdater = rootObject.getAsJsonObject("AutoUpdater")
21-
22-
if (autoUpdater != null) {
23-
val debug = autoUpdater.get("Debug")?.asBoolean ?: false
24-
val clientBranch = autoUpdater.get("Client Branch")?.asString ?: "Snapshot"
25-
val loaderBranch = autoUpdater.get("Loader Branch")?.asString ?: "Stable"
26-
27-
Config(
28-
clientReleaseMode = branchToReleaseMode(clientBranch),
29-
loaderReleaseMode = branchToReleaseMode(loaderBranch),
30-
debug = debug
31-
)
32-
} else Config()
33-
} catch (_: Exception) {
34-
Config()
35-
}
36-
} else Config()
37-
}
14+
private fun loadConfig() = if (configFile.exists()) {
15+
try {
16+
val json = configFile.readText()
17+
val rootObject = gson.fromJson(json, JsonObject::class.java)
18+
19+
val autoUpdater = rootObject.getAsJsonObject("AutoUpdater")
20+
21+
if (autoUpdater != null) {
22+
val debug = autoUpdater.get("Debug")?.asBoolean ?: false
23+
val clientBranch = autoUpdater.get("Client Branch")?.asString ?: "Snapshot"
24+
val loaderBranch = autoUpdater.get("Loader Branch")?.asString ?: "Stable"
25+
26+
Config(
27+
clientReleaseMode = branchToReleaseMode(clientBranch),
28+
loaderReleaseMode = branchToReleaseMode(loaderBranch),
29+
debug = debug
30+
)
31+
} else Config()
32+
} catch (_: Exception) {
33+
Config()
34+
}
35+
} else Config()
3836

3937
private fun branchToReleaseMode(branch: String): ReleaseMode {
4038
return when (branch.uppercase()) {

src/main/kotlin/com/lambda/loader/util/FabricUtil.kt

Lines changed: 83 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import java.nio.file.Path
2020
import java.nio.file.StandardCopyOption
2121
import java.util.logging.Logger
2222
import java.util.zip.ZipFile
23+
import kotlin.io.path.copyTo
24+
import kotlin.io.path.exists
25+
import kotlin.io.path.isRegularFile
26+
import kotlin.io.path.walk
2327

2428
object FabricUtil {
2529
private val logger: Logger = Logger.getLogger("Lambda-Loader")
@@ -72,25 +76,21 @@ object FabricUtil {
7276
FileSystems.newFileSystem(jarUri, mapOf<String, Any>()).use { fs ->
7377
val jarsDir = fs.getPath("META-INF/jars")
7478

75-
if (Files.exists(jarsDir)) {
76-
Files.walk(jarsDir).use { stream ->
77-
stream.filter { path ->
78-
Files.isRegularFile(path) && path.toString().endsWith(".jar")
79-
}.forEach { nestedJarPath ->
80-
if (ConfigManager.config.debug) logger.info("Found nested JAR: $nestedJarPath")
81-
82-
val targetPath = tempDir.resolve(nestedJarPath.fileName.toString())
83-
Files.copy(
84-
nestedJarPath,
85-
targetPath,
86-
StandardCopyOption.REPLACE_EXISTING
87-
)
88-
89-
addToClassPath(targetPath)
90-
nestedJarPaths.add(targetPath)
91-
92-
if (ConfigManager.config.debug) logger.info("Extracted and added nested JAR to classpath: ${nestedJarPath.fileName}")
93-
}
79+
if (!jarsDir.exists()) return@use
80+
81+
jarsDir.walk().filter { path ->
82+
path.isRegularFile() && path.toString().endsWith(".jar")
83+
}.forEach { nestedJarPath ->
84+
if (ConfigManager.config.debug) logger.info("Found nested JAR: $nestedJarPath")
85+
86+
val targetPath = tempDir.resolve(nestedJarPath.fileName.toString())
87+
nestedJarPath.copyTo(targetPath, StandardCopyOption.REPLACE_EXISTING)
88+
89+
addToClassPath(targetPath)
90+
nestedJarPaths.add(targetPath)
91+
92+
if (ConfigManager.config.debug) {
93+
logger.info("Extracted and added nested JAR to classpath: ${nestedJarPath.fileName}")
9494
}
9595
}
9696
}
@@ -130,7 +130,7 @@ object FabricUtil {
130130

131131
// DependencyOverrides takes a Path parameter - use empty path since it isn't used
132132
val depOverrides = DependencyOverrides(Path.of(""))
133-
val metadata = parseMetadataMethod.invoke(
133+
val metadata = parseMetadataMethod(
134134
null, // static method
135135
zip.getInputStream(entry),
136136
jarFile.name,
@@ -153,92 +153,86 @@ object FabricUtil {
153153
/**
154154
* Creates a mod candidate with proper JAR paths, simulating how Fabric Loader does it
155155
*/
156-
private fun createModCandidate(metadata: LoaderModMetadata, jarFile: File): Any? {
157-
return try {
158-
val modCandidateClass = Class.forName("net.fabricmc.loader.impl.discovery.ModCandidateImpl")
159-
160-
val createPlainMethod = modCandidateClass.getDeclaredMethod(
161-
"createPlain",
162-
List::class.java,
163-
LoaderModMetadata::class.java,
164-
Boolean::class.javaPrimitiveType,
165-
Collection::class.java
166-
)
167-
createPlainMethod.isAccessible = true
168-
169-
val candidate = createPlainMethod.invoke(
170-
null, // static method
171-
listOf(jarFile.toPath()), // provide the JAR file path
172-
metadata,
173-
false, // requiresRemap - already in production format
174-
emptyList<Any>() // nested mods
175-
)
176-
177-
if (ConfigManager.config.debug) logger.info("Successfully created mod candidate for ${metadata.id} with JAR path: ${jarFile.absolutePath}")
178-
candidate
179-
} catch (e: Exception) {
180-
logger.severe("Failed to create mod candidate: ${e.message}")
181-
e.printStackTrace()
182-
null
183-
}
156+
private fun createModCandidate(metadata: LoaderModMetadata, jarFile: File) = try {
157+
val modCandidateClass = Class.forName("net.fabricmc.loader.impl.discovery.ModCandidateImpl")
158+
159+
val createPlainMethod = modCandidateClass.getDeclaredMethod(
160+
"createPlain",
161+
List::class.java,
162+
LoaderModMetadata::class.java,
163+
Boolean::class.javaPrimitiveType,
164+
Collection::class.java
165+
)
166+
createPlainMethod.isAccessible = true
167+
168+
val candidate = createPlainMethod(
169+
null, // static method
170+
listOf(jarFile.toPath()), // provide the JAR file path
171+
metadata,
172+
false, // requiresRemap - already in production format
173+
emptyList<Any>() // nested mods
174+
)
175+
176+
if (ConfigManager.config.debug) logger.info("Successfully created mod candidate for ${metadata.id} with JAR path: ${jarFile.absolutePath}")
177+
candidate
178+
} catch (e: Exception) {
179+
logger.severe("Failed to create mod candidate: ${e.message}")
180+
e.printStackTrace()
181+
null
184182
}
185183

186184
/**
187185
* Converts a mod candidate to a mod container
188186
*/
189-
private fun candidateToContainer(candidate: Any): Any? {
190-
return try {
191-
val modContainerClass = Class.forName("net.fabricmc.loader.impl.ModContainerImpl")
192-
val modCandidateClass = Class.forName("net.fabricmc.loader.impl.discovery.ModCandidateImpl")
193-
194-
val constructor = modContainerClass.getDeclaredConstructor(modCandidateClass)
195-
constructor.isAccessible = true
196-
197-
val container = constructor.newInstance(candidate)
198-
if (ConfigManager.config.debug) logger.info("Successfully converted candidate to mod container")
199-
container
200-
} catch (e: Exception) {
201-
logger.severe("Failed to convert candidate to container: ${e.message}")
202-
e.printStackTrace()
203-
null
204-
}
187+
private fun candidateToContainer(candidate: Any) = try {
188+
val modContainerClass = Class.forName("net.fabricmc.loader.impl.ModContainerImpl")
189+
val modCandidateClass = Class.forName("net.fabricmc.loader.impl.discovery.ModCandidateImpl")
190+
191+
val constructor = modContainerClass.getDeclaredConstructor(modCandidateClass)
192+
constructor.isAccessible = true
193+
194+
val container = constructor.newInstance(candidate)
195+
if (ConfigManager.config.debug) logger.info("Successfully converted candidate to mod container")
196+
container
197+
} catch (e: Exception) {
198+
logger.severe("Failed to convert candidate to container: ${e.message}")
199+
e.printStackTrace()
200+
null
205201
}
206202

207203
/**
208204
* Adds a mod container to the Fabric Loader's mod list
209205
*/
210-
private fun addModContainer(container: Any): Boolean {
211-
return try {
212-
val loader = FabricLoaderImpl.INSTANCE
206+
private fun addModContainer(container: Any) = try {
207+
val loader = FabricLoaderImpl.INSTANCE
213208

214-
val loaderClass = loader::class.java
215-
val modsField = loaderClass.getDeclaredField("mods")
216-
modsField.isAccessible = true
209+
val loaderClass = loader::class.java
210+
val modsField = loaderClass.getDeclaredField("mods")
211+
modsField.isAccessible = true
217212

218-
@Suppress("UNCHECKED_CAST")
219-
val modsList = modsField.get(loader) as MutableList<Any>
220-
modsList.add(container)
213+
@Suppress("UNCHECKED_CAST")
214+
val modsList = modsField.get(loader) as MutableList<Any>
215+
modsList.add(container)
221216

222-
val modMapField = loaderClass.getDeclaredField("modMap")
223-
modMapField.isAccessible = true
217+
val modMapField = loaderClass.getDeclaredField("modMap")
218+
modMapField.isAccessible = true
224219

225-
@Suppress("UNCHECKED_CAST")
226-
val modMap = modMapField.get(loader) as MutableMap<String, Any>
227-
val getMetadataMethod = container::class.java.getDeclaredMethod("getMetadata")
220+
@Suppress("UNCHECKED_CAST")
221+
val modMap = modMapField.get(loader) as MutableMap<String, Any>
222+
val getMetadataMethod = container::class.java.getDeclaredMethod("getMetadata")
228223

229-
getMetadataMethod.isAccessible = true
230-
val metadata = getMetadataMethod.invoke(container) as LoaderModMetadata
231-
val modId = metadata.id
224+
getMetadataMethod.isAccessible = true
225+
val metadata = getMetadataMethod.invoke(container) as LoaderModMetadata
226+
val modId = metadata.id
232227

233-
modMap[modId] = container
228+
modMap[modId] = container
234229

235-
if (ConfigManager.config.debug) logger.info("Successfully added mod container for $modId to Fabric Loader")
236-
true
237-
} catch (e: Exception) {
238-
logger.severe("Failed to add mod container to Fabric Loader: ${e.message}")
239-
e.printStackTrace()
240-
false
241-
}
230+
if (ConfigManager.config.debug) logger.info("Successfully added mod container for $modId to Fabric Loader")
231+
true
232+
} catch (e: Exception) {
233+
logger.severe("Failed to add mod container to Fabric Loader: ${e.message}")
234+
e.printStackTrace()
235+
false
242236
}
243237

244238
/**

0 commit comments

Comments
 (0)