Skip to content

Commit 6e5a5a8

Browse files
committed
Modify config manager to use Lambda module config for loader config and fallback to stable releases if config isn't found
1 parent 4840dea commit 6e5a5a8

File tree

3 files changed

+44
-41
lines changed

3 files changed

+44
-41
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class LoaderVersionController(
4848

4949
if (loaderMod.isPresent) {
5050
val version = loaderMod.get().metadata.version.friendlyString
51-
if (com.lambda.loader.config.ConfigManager.config.debug) {
51+
if (ConfigManager.config.debug) {
5252
logger.info("Current loader version: $version")
5353
}
5454
return version
@@ -85,7 +85,7 @@ class LoaderVersionController(
8585
val currentVersion = getCurrentLoaderVersion()
8686

8787
// Try to get latest version based on release mode, with fallback
88-
var latestVersion = when (getReleaseMode()) {
88+
val latestVersion = when (getReleaseMode()) {
8989
ReleaseMode.STABLE -> {
9090
val releaseVersion = checkReleasesVersion()
9191
if (releaseVersion == null) {

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,55 @@ package com.lambda.loader.config
22

33
import com.google.gson.Gson
44
import com.google.gson.GsonBuilder
5+
import com.google.gson.JsonObject
56
import java.io.File
67

78
object ConfigManager {
89

9-
private val configFile: File = File("lambda", "loader.json")
10+
private val configFile: File = File("lambda/config", "modules.json")
1011
private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
1112

1213
var config: Config = loadConfig()
13-
private set
1414

1515
private fun loadConfig(): Config {
1616
return if (configFile.exists()) {
1717
try {
1818
val json = configFile.readText()
19-
val loadedConfig = gson.fromJson(json, Config::class.java)
20-
21-
// Merge with defaults to handle new fields
22-
val defaultConfig = Config()
23-
val mergedConfig = Config(
24-
clientReleaseMode = loadedConfig?.clientReleaseMode ?: defaultConfig.clientReleaseMode,
25-
loaderReleaseMode = loadedConfig?.loaderReleaseMode ?: defaultConfig.loaderReleaseMode,
26-
debug = loadedConfig?.debug ?: defaultConfig.debug
27-
)
28-
29-
// Save merged config to add any new fields
30-
saveConfig(mergedConfig)
31-
mergedConfig
32-
} catch (e: Exception) {
19+
val rootObject = gson.fromJson(json, JsonObject::class.java)
20+
21+
// Extract AutoUpdater object
22+
val autoUpdater = rootObject.getAsJsonObject("AutoUpdater")
23+
24+
if (autoUpdater != null) {
25+
val debug = autoUpdater.get("Debug")?.asBoolean ?: false
26+
val loaderBranch = autoUpdater.get("Loader Branch")?.asString ?: "RELEASE"
27+
val clientBranch = autoUpdater.get("Client Branch")?.asString ?: "RELEASE"
28+
29+
Config(
30+
clientReleaseMode = branchToReleaseMode(clientBranch),
31+
loaderReleaseMode = branchToReleaseMode(loaderBranch),
32+
debug = debug
33+
)
34+
} else {
35+
// AutoUpdater section doesn't exist, use defaults
36+
Config()
37+
}
38+
} catch (_: Exception) {
3339
// If parsing fails, use defaults
34-
val defaultConfig = Config()
35-
saveConfig(defaultConfig)
36-
defaultConfig
40+
Config()
3741
}
3842
} else {
39-
val defaultConfig = Config()
40-
saveConfig(defaultConfig)
41-
defaultConfig
43+
// Config file doesn't exist, use defaults
44+
Config()
4245
}
4346
}
4447

45-
fun saveConfig(config: Config) {
46-
if (!configFile.parentFile.exists()) {
47-
configFile.parentFile.mkdirs()
48+
private fun branchToReleaseMode(branch: String): ReleaseMode {
49+
return when (branch.uppercase()) {
50+
"RELEASE", "STABLE" -> ReleaseMode.STABLE
51+
"SNAPSHOT" -> ReleaseMode.SNAPSHOT
52+
else -> ReleaseMode.STABLE
4853
}
49-
val json = gson.toJson(config)
50-
configFile.writeText(json)
51-
this.config = config
5254
}
55+
5356
}

src/test/kotlin/com/lambda/loader/VersionControllerTest.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class VersionControllerTest {
9090

9191
// Test STABLE mode
9292
println("Checking STABLE releases for MC $mcVersion...")
93-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE))
93+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE)
9494

9595
val stableJar = versionController.getOrDownloadLatestVersion()
9696
if (stableJar != null) {
@@ -105,7 +105,7 @@ class VersionControllerTest {
105105

106106
// Test SNAPSHOT mode
107107
println("Checking SNAPSHOT releases for MC $mcVersion...")
108-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.SNAPSHOT))
108+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.SNAPSHOT)
109109

110110
val snapshotJar = versionController.getOrDownloadLatestVersion()
111111
if (snapshotJar != null) {
@@ -136,7 +136,7 @@ class VersionControllerTest {
136136
val versionController = VersionController(minecraftVersionOverride = mcVersion)
137137

138138
// Set to STABLE mode
139-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE))
139+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE)
140140

141141
val jarFile = versionController.getOrDownloadLatestVersion()
142142

@@ -166,7 +166,7 @@ class VersionControllerTest {
166166
val versionController = VersionController(minecraftVersionOverride = mcVersion)
167167

168168
// Set to SNAPSHOT mode
169-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.SNAPSHOT))
169+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.SNAPSHOT)
170170

171171
val jarFile = versionController.getOrDownloadLatestVersion()
172172

@@ -194,7 +194,7 @@ class VersionControllerTest {
194194

195195
val versionController = VersionController(minecraftVersionOverride = mcVersion)
196196

197-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE))
197+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE)
198198

199199
// First call - should download
200200
val jarFile1 = versionController.getOrDownloadLatestVersion()
@@ -224,7 +224,7 @@ class VersionControllerTest {
224224

225225
val versionController = VersionController(minecraftVersionOverride = mcVersion)
226226

227-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE))
227+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE)
228228

229229
val jarFile = versionController.getOrDownloadLatestVersion()
230230
assertNotNull(jarFile, "JAR file should not be null")
@@ -250,13 +250,13 @@ class VersionControllerTest {
250250
val versionController = VersionController(minecraftVersionOverride = mcVersion)
251251

252252
// Get STABLE version
253-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE))
253+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE)
254254
val stableJar = versionController.getOrDownloadLatestVersion()
255255
assertNotNull(stableJar, "STABLE JAR should not be null")
256256
println("STABLE: ${stableJar!!.name}")
257257

258258
// Switch to SNAPSHOT
259-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.SNAPSHOT))
259+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.SNAPSHOT)
260260
val snapshotJar = versionController.getOrDownloadLatestVersion()
261261
assertNotNull(snapshotJar, "SNAPSHOT JAR should not be null")
262262
println("SNAPSHOT: ${snapshotJar!!.name}")
@@ -284,7 +284,7 @@ class VersionControllerTest {
284284
val versionController = VersionController(minecraftVersionOverride = mcVersion)
285285

286286
// Test SNAPSHOT mode (more likely to have versions available)
287-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.SNAPSHOT))
287+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.SNAPSHOT)
288288
val jarFile = versionController.getOrDownloadLatestVersion()
289289

290290
if (jarFile != null) {
@@ -329,7 +329,7 @@ class VersionControllerTest {
329329
val versionController = VersionController(minecraftVersionOverride = mcVersion)
330330

331331
// Set to STABLE mode - should fallback to snapshot if stable doesn't exist
332-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE, debug = true))
332+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE, debug = true)
333333

334334
val jarFile = versionController.getOrDownloadLatestVersion()
335335

@@ -355,7 +355,7 @@ class VersionControllerTest {
355355
val versionController = VersionController(minecraftVersionOverride = nonExistentVersion)
356356

357357
// Set to STABLE mode
358-
ConfigManager.saveConfig(ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE, debug = true))
358+
ConfigManager.config = ConfigManager.config.copy(clientReleaseMode = ReleaseMode.STABLE, debug = true)
359359

360360
val jarFile = versionController.getOrDownloadLatestVersion()
361361

0 commit comments

Comments
 (0)