@@ -20,6 +20,10 @@ import java.nio.file.Path
2020import java.nio.file.StandardCopyOption
2121import java.util.logging.Logger
2222import 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
2428object 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