-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
src/main/kotlin/su/plo/voice/plugin/entrypoint/FabricAddonEntryPoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/kotlin/su/plo/voice/plugin/entrypoint/MinestomAddonEntryPoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package su.plo.voice.plugin.entrypoint | ||
|
||
import com.google.common.base.CaseFormat | ||
import com.google.gson.GsonBuilder | ||
import com.google.gson.JsonArray | ||
import com.google.gson.JsonObject | ||
import org.gradle.api.Project | ||
import org.objectweb.asm.ClassWriter | ||
import org.objectweb.asm.Opcodes | ||
import su.plo.voice.plugin.AddonMeta | ||
import su.plo.voice.plugin.extension.visitCodeThenEnd | ||
import java.io.File | ||
|
||
object MinestomAddonEntryPoint : AddonEntryPoint() { | ||
|
||
private val gson = GsonBuilder().setPrettyPrinting().create() | ||
|
||
override fun generate(project: Project, addons: List<AddonMeta>) { | ||
val serverAddons = addons.filter { it.loaderScope.isServer } | ||
if (serverAddons.isEmpty()) return | ||
|
||
val packageName = serverAddons[0].entryPoint.substringBeforeLast(".") | ||
|
||
generateJavaFile(project, packageName, serverAddons) | ||
generateMetaFile(project, packageName, serverAddons) | ||
} | ||
|
||
override fun generateMetaFile(project: Project, packageName: String, addons: List<AddonMeta>) { | ||
val addon = addons[0] | ||
|
||
val fabricJson = JsonObject() | ||
|
||
fabricJson.addProperty("name", transformAddonId(addon.id)) | ||
fabricJson.addProperty("entrypoint", "$packageName.MinestomEntryPoint") | ||
fabricJson.addProperty("version", addon.version) | ||
fabricJson.add("dependencies", JsonArray().also { depends -> | ||
depends.add("PlasmoVoice") | ||
addon.dependencies.filter { !it.isOptional }.forEach { dependency -> | ||
depends.add(transformAddonId(dependency.id)) | ||
} | ||
}) | ||
|
||
val resourcesDir = File( | ||
project.buildDir, | ||
"generated/sources/plasmovoice/resources" | ||
).also { it.mkdirs() } | ||
|
||
File( | ||
resourcesDir, | ||
"extension.json" | ||
).writeText(gson.toJson(fabricJson)) | ||
} | ||
|
||
override fun generateJavaFile(project: Project, packageName: String, addons: List<AddonMeta>) { | ||
// Define class | ||
val className = "MinestomEntryPoint" | ||
val fullClassName = "${packageName.replace(".", "/")}/$className" | ||
val classOwner = "net/minestom/server/extensions/Extension" | ||
|
||
val cw = ClassWriter(ClassWriter.COMPUTE_FRAMES) | ||
|
||
cw.visit( | ||
Opcodes.V1_8, | ||
Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, | ||
fullClassName, | ||
null, | ||
classOwner, | ||
null | ||
) | ||
|
||
// Add constructor | ||
val constructorMethod = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null) | ||
constructorMethod.visitCodeThenEnd { | ||
generateConstructor(cw, constructorMethod, fullClassName, classOwner, addons) | ||
} | ||
|
||
// Add initialize method | ||
val loadMethod = cw.visitMethod(Opcodes.ACC_PUBLIC, "initialize", "()V", null, null) | ||
loadMethod.visitCodeThenEnd { | ||
generateServerAddonsLoaders(loadMethod, fullClassName, addons) | ||
} | ||
|
||
// Add terminate method | ||
val unloadMethod = cw.visitMethod(Opcodes.ACC_PUBLIC, "terminate", "()V", null, null) | ||
unloadMethod.visitCodeThenEnd { | ||
generateServerAddonsUnloaders(unloadMethod, fullClassName, addons) | ||
} | ||
|
||
// Generate bytecode | ||
cw.visitEnd() | ||
|
||
val packageDir = File( | ||
project.buildDir, | ||
"classes/java/main/${packageName.replace(".", "/")}" | ||
).also { it.mkdirs() } | ||
|
||
File( | ||
packageDir, | ||
"${className}.class" | ||
).writeBytes(cw.toByteArray()) | ||
} | ||
|
||
private fun transformAddonId(addonId: String): String = | ||
CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, addonId) | ||
} |