Skip to content

Commit 8f358f9

Browse files
committed
Make it read access widener and mixins from fabric.mod.json instead of using hardcoded values
1 parent d2e4ec3 commit 8f358f9

2 files changed

Lines changed: 85 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ class LambdaLoaderInitializer : PreLaunchEntrypoint {
1717
FabricUtil.addToClassPath(latestVersion)
1818
FabricUtil.loadNestedJars(latestVersion)
1919
FabricUtil.registerModFromJar(latestVersion)
20-
FabricUtil.injectAccessWidener()
21-
Mixins.addConfiguration("lambda.mixins.common.json")
20+
val accessWidenerPath = FabricUtil.getAccessWidenerFileName(latestVersion);
21+
if(accessWidenerPath != null) FabricUtil.injectAccessWidener(accessWidenerPath)
22+
for (mixinFile in FabricUtil.getMixinFileNames(latestVersion)) {
23+
Mixins.addConfiguration(mixinFile)
24+
}
2225

2326
}
2427

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.lambda.lambdaLoader.util
22

3+
import com.google.gson.JsonObject
4+
import com.google.gson.JsonParser
35
import com.lambda.lambdaLoader.config.ConfigManager
46
import net.fabricmc.loader.impl.FabricLoaderImpl
57
import net.fabricmc.loader.impl.launch.FabricLauncherBase
@@ -9,6 +11,7 @@ import net.fabricmc.loader.impl.metadata.DependencyOverrides
911
import net.fabricmc.loader.impl.metadata.LoaderModMetadata
1012
import net.fabricmc.loader.impl.metadata.VersionOverrides
1113
import java.io.File
14+
import java.io.InputStreamReader
1215
import java.nio.file.FileSystems
1316
import java.nio.file.Files
1417
import java.nio.file.Path
@@ -20,7 +23,7 @@ object FabricUtil {
2023

2124
private val logger: Logger = Logger.getLogger("Lambda-Loader")
2225

23-
fun injectAccessWidener(resourcePath: String = "lambda.accesswidener", namespace: String = "intermediary") {
26+
fun injectAccessWidener(resourcePath: String, namespace: String = "intermediary") {
2427
try {
2528
// Get the existing ClassTweaker from FabricLoader
2629
val classTweaker = FabricLoaderImpl.INSTANCE.classTweaker as ClassTweakerImpl
@@ -268,6 +271,82 @@ object FabricUtil {
268271
}
269272
}
270273

274+
/**
275+
* Reads the fabric.mod.json from a JAR file and returns it as a JsonObject
276+
*/
277+
fun readFabricModJson(jarFile: File): JsonObject? {
278+
return try {
279+
ZipFile(jarFile).use { zip ->
280+
val entry = zip.getEntry("fabric.mod.json")
281+
?: throw IllegalArgumentException("No fabric.mod.json found in JAR: ${jarFile.name}")
282+
283+
val inputStream = zip.getInputStream(entry)
284+
val reader = InputStreamReader(inputStream, Charsets.UTF_8)
285+
val jsonObject = JsonParser.parseReader(reader).asJsonObject
286+
287+
if (ConfigManager.config.debug) {
288+
logger.info("Successfully read fabric.mod.json from ${jarFile.name}")
289+
}
290+
jsonObject
291+
}
292+
} catch (e: Exception) {
293+
logger.severe("Failed to read fabric.mod.json from ${jarFile.name}: ${e.message}")
294+
e.printStackTrace()
295+
null
296+
}
297+
}
298+
299+
/**
300+
* Gets the access widener file name from fabric.mod.json
301+
* Returns null if no access widener is specified
302+
*/
303+
fun getAccessWidenerFileName(jarFile: File): String? {
304+
return try {
305+
val json = readFabricModJson(jarFile) ?: return null
306+
307+
val accessWidener = json.get("accessWidener")?.asString
308+
309+
if (ConfigManager.config.debug && accessWidener != null) {
310+
logger.info("Found access widener: $accessWidener in ${jarFile.name}")
311+
}
312+
313+
accessWidener
314+
} catch (e: Exception) {
315+
logger.severe("Failed to get access widener from ${jarFile.name}: ${e.message}")
316+
e.printStackTrace()
317+
null
318+
}
319+
}
320+
321+
/**
322+
* Gets the list of mixin configuration files from fabric.mod.json
323+
* Returns an empty list if no mixins are specified
324+
*/
325+
fun getMixinFileNames(jarFile: File): List<String> {
326+
return try {
327+
val json = readFabricModJson(jarFile) ?: return emptyList()
328+
329+
val mixins = mutableListOf<String>()
330+
331+
// Check for "mixins" field (array of strings)
332+
json.get("mixins")?.asJsonArray?.forEach { element ->
333+
if (element.isJsonPrimitive) {
334+
mixins.add(element.asString)
335+
}
336+
}
337+
338+
if (ConfigManager.config.debug && mixins.isNotEmpty()) {
339+
logger.info("Found ${mixins.size} mixin config(s) in ${jarFile.name}: ${mixins.joinToString(", ")}")
340+
}
341+
342+
mixins
343+
} catch (e: Exception) {
344+
logger.severe("Failed to get mixins from ${jarFile.name}: ${e.message}")
345+
e.printStackTrace()
346+
emptyList()
347+
}
348+
}
349+
271350
/**
272351
* Convenience method to load metadata from JAR, create a mod candidate,
273352
* convert to container, and add to Fabric Loader

0 commit comments

Comments
 (0)