forked from lambda-client/lambda-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoaderVersionController.kt
More file actions
126 lines (112 loc) · 4.5 KB
/
LoaderVersionController.kt
File metadata and controls
126 lines (112 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.lambda.loader
import com.lambda.loader.config.ConfigManager
import com.lambda.loader.config.ReleaseMode
import java.net.URI
import java.net.URL
/**
* Version controller for Lambda-Loader self-updates.
* Fetches loader updates from a separate Maven repository.
*/
class LoaderVersionController(
cache: Cache = Cache(),
/**
* The maven repository URL for the loader artifacts.
* Can be overridden for testing or custom repositories.
*/
loaderMavenUrl: String = "https://maven.lambda-client.org"
) : BaseMavenVersionController(cache, versionMatchingEnabled = false) {
override val mavenUrl: String = loaderMavenUrl
override val releasesMetaUrl: URL =
URI("$mavenUrl/releases/com/lambda/loader/maven-metadata.xml").toURL()
override val snapshotMetaUrl: URL =
URI("$mavenUrl/snapshots/com/lambda/loader/maven-metadata.xml").toURL()
override val artifactPath: String = "com/lambda/loader"
override val artifactName: String = "loader"
/**
* Loader doesn't need version matching - we always want the latest version
*/
override fun getVersionToMatch(): String? = null
/**
* Use the loader-specific release mode from config
*/
override fun getReleaseMode(): ReleaseMode = ConfigManager.config.loaderReleaseMode
/**
* Get the current loader version from the Fabric mod metadata.
* This can be used to check if an update is available.
*/
fun getCurrentLoaderVersion(): String? {
return try {
// Try to read from Fabric mod container
val fabricLoader = net.fabricmc.loader.api.FabricLoader.getInstance()
val loaderMod = fabricLoader.getModContainer("lambda-loader")
if (loaderMod.isPresent) {
val version = loaderMod.get().metadata.version.friendlyString
if (ConfigManager.config.debug) {
logger.info("Current loader version: $version")
}
return version
}
// Fallback: try package implementation version
val version = this::class.java.`package`?.implementationVersion
if (version != null) {
if (ConfigManager.config.debug) {
logger.info("Current loader version (from package): $version")
}
return version
}
if (ConfigManager.config.debug) {
logger.warning("Could not determine current loader version")
}
null
} catch (e: Exception) {
if (ConfigManager.config.debug) {
logger.warning("Error reading loader version: ${e.message}")
e.printStackTrace()
}
null
}
}
/**
* Check if an update is available by comparing current version with latest available.
* Returns the latest version if an update is available, null otherwise.
*/
fun checkForUpdate(): String? {
return try {
val currentVersion = getCurrentLoaderVersion()
// Try to get latest version based on release mode, with fallback
val latestVersion = when (getReleaseMode()) {
ReleaseMode.STABLE -> {
val releaseVersion = checkReleasesVersion()
if (releaseVersion == null) {
logger.warning("No stable loader version found, falling back to snapshot")
checkSnapshotVersion()
} else {
releaseVersion
}
}
ReleaseMode.SNAPSHOT -> checkSnapshotVersion()
}
if (latestVersion == null) {
logger.warning("Could not fetch latest loader version")
return null
}
if (currentVersion == null) {
logger.info("Latest loader version available: $latestVersion")
return latestVersion
}
if (currentVersion != latestVersion) {
logger.info("Loader update available: $currentVersion -> $latestVersion")
return latestVersion
} else {
if (ConfigManager.config.debug) {
logger.info("Loader is up to date: $currentVersion")
}
return null
}
} catch (e: Exception) {
logger.severe("Error checking for loader update: ${e.message}")
e.printStackTrace()
null
}
}
}