forked from lambda-client/lambda-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFabricUtil.kt
More file actions
368 lines (313 loc) · 14.2 KB
/
FabricUtil.kt
File metadata and controls
368 lines (313 loc) · 14.2 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package com.lambda.loader.util
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.lambda.loader.config.ConfigManager
import net.fabricmc.loader.impl.FabricLoaderImpl
import net.fabricmc.loader.impl.launch.FabricLauncherBase
import net.fabricmc.loader.impl.lib.classtweaker.impl.ClassTweakerImpl
import net.fabricmc.loader.impl.lib.classtweaker.reader.ClassTweakerReaderImpl
import net.fabricmc.loader.impl.metadata.DependencyOverrides
import net.fabricmc.loader.impl.metadata.LoaderModMetadata
import net.fabricmc.loader.impl.metadata.VersionOverrides
import java.io.File
import java.io.InputStreamReader
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import java.util.logging.Logger
import java.util.zip.ZipFile
object FabricUtil {
private val logger: Logger = Logger.getLogger("Lambda-Loader")
fun injectAccessWidener(resourcePath: String, namespace: String = "intermediary") {
try {
// Get the existing ClassTweaker from FabricLoader
val classTweaker = FabricLoaderImpl.INSTANCE.classTweaker as ClassTweakerImpl
// Create a reader with the ClassTweaker as the visitor
val reader = ClassTweakerReaderImpl(classTweaker)
// Read from resource
val content = this::class.java.classLoader.getResourceAsStream(resourcePath)
?.readBytes()
?: throw IllegalArgumentException("Access widener file not found: $resourcePath")
// Read into the ClassTweaker (which creates AccessWidenerImpl instances internally)
reader.read(content, namespace)
if (ConfigManager.config.debug) {
logger.info("Successfully loaded access widener from $resourcePath")
}
} catch (e: Exception) {
logger.severe("Failed to inject access widener: ${e.message}")
e.printStackTrace()
}
}
fun addToClassPath(jarFile: File) {
FabricLauncherBase.getLauncher().addToClassPath(jarFile.toPath())
}
fun addToClassPath(jarPath: Path) {
FabricLauncherBase.getLauncher().addToClassPath(jarPath)
}
/**
* Loads nested JARs from META-INF/jars/ directory and adds them to classpath
* Fabric Loader stores bundled dependencies in this directory for jar-in-jar functionality
* Extracts nested JARs to a consistent temp directory that's cleared on each run
*/
fun loadNestedJars(jarFile: File): List<Path> {
val nestedJarPaths = mutableListOf<Path>()
try {
// Use a consistent temp directory location
val tempDir = Path.of(System.getProperty("java.io.tmpdir"), "lambda-loader-nested")
// Clear the directory if it exists, then recreate it
if (Files.exists(tempDir)) {
Files.walk(tempDir)
.sorted(Comparator.reverseOrder())
.forEach { Files.deleteIfExists(it) }
}
Files.createDirectories(tempDir)
if (ConfigManager.config.debug) {
logger.info("Created temp directory for nested JARs: $tempDir")
}
// Create URI for the JAR file system
val jarUri = java.net.URI.create("jar:${jarFile.toURI()}")
// Open the JAR as a file system
FileSystems.newFileSystem(jarUri, mapOf<String, Any>()).use { fs ->
val jarsDir = fs.getPath("META-INF/jars")
if (Files.exists(jarsDir)) {
Files.walk(jarsDir).use { stream ->
stream.filter { path ->
Files.isRegularFile(path) && path.toString().endsWith(".jar")
}.forEach { nestedJarPath ->
if (ConfigManager.config.debug) {
logger.info("Found nested JAR: $nestedJarPath")
}
// Extract to temp directory
val targetPath = tempDir.resolve(nestedJarPath.fileName.toString())
Files.copy(
nestedJarPath,
targetPath,
StandardCopyOption.REPLACE_EXISTING
)
// Add extracted JAR to classpath
addToClassPath(targetPath)
nestedJarPaths.add(targetPath)
if (ConfigManager.config.debug) {
logger.info("Extracted and added nested JAR to classpath: ${nestedJarPath.fileName}")
}
}
}
}
}
if (ConfigManager.config.debug) {
logger.info("Successfully loaded ${nestedJarPaths.size} nested JARs from ${jarFile.name}")
}
} catch (e: Exception) {
logger.severe("Failed to load nested JARs: ${e.message}")
e.printStackTrace()
}
return nestedJarPaths
}
/**
* Loads mod metadata from a JAR file by parsing its fabric.mod.json
*/
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
fun loadMetadataFromJar(jarFile: File): LoaderModMetadata? {
return try {
ZipFile(jarFile).use { zip ->
val entry = zip.getEntry("fabric.mod.json")
?: throw IllegalArgumentException("No fabric.mod.json found in JAR")
// Use ModMetadataParser to parse the metadata
val parserClass = Class.forName("net.fabricmc.loader.impl.metadata.ModMetadataParser")
val parseMetadataMethod = parserClass.getDeclaredMethod(
"parseMetadata",
java.io.InputStream::class.java,
String::class.java,
java.util.List::class.java,
Class.forName("net.fabricmc.loader.impl.metadata.VersionOverrides"),
Class.forName("net.fabricmc.loader.impl.metadata.DependencyOverrides"),
Boolean::class.javaPrimitiveType
)
parseMetadataMethod.isAccessible = true
val versionOverrides = VersionOverrides()
// DependencyOverrides takes a Path parameter - use empty path since it isn't used
val depOverrides = DependencyOverrides(Path.of(""))
val metadata = parseMetadataMethod.invoke(
null, // static method
zip.getInputStream(entry),
jarFile.name,
emptyList<String>(), // parent mod ids
versionOverrides,
depOverrides,
false // isDevelopment
) as LoaderModMetadata
if (ConfigManager.config.debug) {
logger.info("Successfully loaded metadata from JAR: ${metadata.id}")
}
metadata
}
} catch (e: Exception) {
logger.severe("Failed to load metadata from JAR: ${e.message}")
e.printStackTrace()
null
}
}
/**
* Creates a mod candidate with proper JAR paths, simulating how Fabric Loader does it
*/
private fun createModCandidate(metadata: LoaderModMetadata, jarFile: File): Any? {
return try {
val modCandidateClass = Class.forName("net.fabricmc.loader.impl.discovery.ModCandidateImpl")
// Use the createPlain static factory method
val createPlainMethod = modCandidateClass.getDeclaredMethod(
"createPlain",
List::class.java,
LoaderModMetadata::class.java,
Boolean::class.javaPrimitiveType,
Collection::class.java
)
createPlainMethod.isAccessible = true
// Create a mod candidate with the JAR file path - just like Fabric Loader does
val candidate = createPlainMethod.invoke(
null, // static method
listOf(jarFile.toPath()), // provide the JAR file path
metadata,
false, // requiresRemap - already in production format
emptyList<Any>() // nested mods
)
if (ConfigManager.config.debug) {
logger.info("Successfully created mod candidate for ${metadata.id} with JAR path: ${jarFile.absolutePath}")
}
candidate
} catch (e: Exception) {
logger.severe("Failed to create mod candidate: ${e.message}")
e.printStackTrace()
null
}
}
/**
* Converts a mod candidate to a mod container
*/
private fun candidateToContainer(candidate: Any): Any? {
return try {
val modContainerClass = Class.forName("net.fabricmc.loader.impl.ModContainerImpl")
val modCandidateClass = Class.forName("net.fabricmc.loader.impl.discovery.ModCandidateImpl")
// ModContainerImpl(ModCandidateImpl candidate)
val constructor = modContainerClass.getDeclaredConstructor(modCandidateClass)
constructor.isAccessible = true
val container = constructor.newInstance(candidate)
if (ConfigManager.config.debug) {
logger.info("Successfully converted candidate to mod container")
}
container
} catch (e: Exception) {
logger.severe("Failed to convert candidate to container: ${e.message}")
e.printStackTrace()
null
}
}
/**
* Adds a mod container to the Fabric Loader's mod list
*/
private fun addModContainer(container: Any): Boolean {
return try {
val loader = FabricLoaderImpl.INSTANCE
// Get the mods field
val loaderClass = loader::class.java
val modsField = loaderClass.getDeclaredField("mods")
modsField.isAccessible = true
@Suppress("UNCHECKED_CAST")
val modsList = modsField.get(loader) as MutableList<Any>
modsList.add(container)
// Also need to add to modMap (Map<String, ModContainerImpl>)also
val modMapField = loaderClass.getDeclaredField("modMap")
modMapField.isAccessible = true
@Suppress("UNCHECKED_CAST")
val modMap = modMapField.get(loader) as MutableMap<String, Any>
// Get the mod ID from the container
val getMetadataMethod = container::class.java.getDeclaredMethod("getMetadata")
getMetadataMethod.isAccessible = true
val metadata = getMetadataMethod.invoke(container) as LoaderModMetadata
val modId = metadata.id
modMap[modId] = container
if (ConfigManager.config.debug) {
logger.info("Successfully added mod container for $modId to Fabric Loader")
}
true
} catch (e: Exception) {
logger.severe("Failed to add mod container to Fabric Loader: ${e.message}")
e.printStackTrace()
false
}
}
/**
* Reads the fabric.mod.json from a JAR file and returns it as a JsonObject
*/
fun readFabricModJson(jarFile: File): JsonObject? {
return try {
ZipFile(jarFile).use { zip ->
val entry = zip.getEntry("fabric.mod.json")
?: throw IllegalArgumentException("No fabric.mod.json found in JAR: ${jarFile.name}")
val inputStream = zip.getInputStream(entry)
val reader = InputStreamReader(inputStream, Charsets.UTF_8)
val jsonObject = JsonParser.parseReader(reader).asJsonObject
if (ConfigManager.config.debug) {
logger.info("Successfully read fabric.mod.json from ${jarFile.name}")
}
jsonObject
}
} catch (e: Exception) {
logger.severe("Failed to read fabric.mod.json from ${jarFile.name}: ${e.message}")
e.printStackTrace()
null
}
}
/**
* Gets the access widener file name from fabric.mod.json
* Returns null if no access widener is specified
*/
fun getAccessWidenerFileName(jarFile: File): String? {
return try {
val json = readFabricModJson(jarFile) ?: return null
val accessWidener = json.get("accessWidener")?.asString
if (ConfigManager.config.debug && accessWidener != null) {
logger.info("Found access widener: $accessWidener in ${jarFile.name}")
}
accessWidener
} catch (e: Exception) {
logger.severe("Failed to get access widener from ${jarFile.name}: ${e.message}")
e.printStackTrace()
null
}
}
/**
* Gets the list of mixin configuration files from fabric.mod.json
* Returns an empty list if no mixins are specified
*/
fun getMixinFileNames(jarFile: File): List<String> {
return try {
val json = readFabricModJson(jarFile) ?: return emptyList()
val mixins = mutableListOf<String>()
// Check for "mixins" field (array of strings)
json.get("mixins")?.asJsonArray?.forEach { element ->
if (element.isJsonPrimitive) {
mixins.add(element.asString)
}
}
if (ConfigManager.config.debug && mixins.isNotEmpty()) {
logger.info("Found ${mixins.size} mixin config(s) in ${jarFile.name}: ${mixins.joinToString(", ")}")
}
mixins
} catch (e: Exception) {
logger.severe("Failed to get mixins from ${jarFile.name}: ${e.message}")
e.printStackTrace()
emptyList()
}
}
/**
* Convenience method to load metadata from JAR, create a mod candidate,
* convert to container, and add to Fabric Loader
*/
fun registerModFromJar(jarFile: File): Boolean {
val metadata = loadMetadataFromJar(jarFile) ?: return false
val candidate = createModCandidate(metadata, jarFile) ?: return false
val container = candidateToContainer(candidate) ?: return false
return addModContainer(container)
}
}