11package com.lambda.lambdaLoader.util
22
3+ import com.google.gson.JsonObject
4+ import com.google.gson.JsonParser
35import com.lambda.lambdaLoader.config.ConfigManager
46import net.fabricmc.loader.impl.FabricLoaderImpl
57import net.fabricmc.loader.impl.launch.FabricLauncherBase
@@ -9,6 +11,7 @@ import net.fabricmc.loader.impl.metadata.DependencyOverrides
911import net.fabricmc.loader.impl.metadata.LoaderModMetadata
1012import net.fabricmc.loader.impl.metadata.VersionOverrides
1113import java.io.File
14+ import java.io.InputStreamReader
1215import java.nio.file.FileSystems
1316import java.nio.file.Files
1417import 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