Replace the reflections8 library (and therefore javassist) using FabricLoader

This commit is contained in:
Username404 2021-06-28 22:56:07 +02:00
parent f3d4770e00
commit 3290db57a9
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
4 changed files with 24 additions and 19 deletions

View File

@ -84,8 +84,6 @@ subprojects {
}
withType(ShadowJar::class) {
this.configurations = listOf(shadowC)
relocate("javassist", "${rootProject.group}.javassist")
relocate("org.reflections8", "${rootProject.group}.reflectionlib")
relocate("com.typesafe.config", "${rootProject.group}.typesafe.config")
relocate("io.github.config4k", "${rootProject.group}.config4k")
relocate("net.arikia.dev.drpc", "${rootProject.group}.drpc")

View File

@ -38,11 +38,11 @@ val SnowyConfigScreen: Screen = object: Screen(translationComponent) {
override fun onClose() { minecraft?.screen = configScreenParent }
}; get() = try {
Class.forName("me.shedaniel.clothconfig2.api.ConfigBuilder")
val macrosBox: ClickBox = ClickGui.components.find {
val macrosBox: ClickBox? = ClickGui.components.find {
(it is ClickBox) && it.isCategory(Category.MACROS)
} as ClickBox
} as? ClickBox
@Suppress("UNCHECKED_CAST")
val macrosButtons = macrosBox.buttons as MutableSet<Macro>
val macrosButtons = (macrosBox?.buttons ?: LinkedHashSet<Macro>()) as MutableSet<Macro>
fun Collection<Macro>.getTitleCommand(): MutableList<String> = map { it.run { "$title: $command" } }.toMutableList()
me.shedaniel.clothconfig2.api.ConfigBuilder.create().setParentScreen(configScreenParent).transparentBackground()
.setShouldListSmoothScroll(true)
@ -109,7 +109,7 @@ val SnowyConfigScreen: Screen = object: Screen(translationComponent) {
macrosButtons.add(newMacro)
}
}
}; if (it.isEmpty().also { empty -> macrosBox.hidden = empty })
}; if (it.isEmpty().also { empty -> macrosBox?.hidden = empty })
macrosButtons.clear()
}.build()
)

View File

@ -19,7 +19,6 @@ dependencies {
modApi("me.shedaniel.cloth:cloth-config-fabric:${rootProject.property("clothconfig_version")}") { exclude(group = Groups.FabricApi) }
include(modRuntime("${Groups.FabricApi}:fabric-resource-loader-v0:${rootProject.property("fabric_resource_loader_version")}")!!)
modImplementation(group = "net.fabricmc", name = "fabric-language-kotlin", version = rootProject.property("fabric_language_kotlin") as String)
listOf("net.oneandone.reflections8:reflections8:0.11.7", "org.javassist:javassist:3.28.0-GA").forEach { implementation(it); shadowC(it) { isTransitive = false } }
modRuntime(modCompileOnly("com.terraformersmc:modmenu:${rootProject.property("modmenu_version")}") {
exclude(group = Groups.FabricApi, module = "fabric-api-base")
exclude(group = Groups.FabricApi, module = "fabric-resource-loader-v0")

View File

@ -4,15 +4,15 @@ import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.EventSnowy
import fr.username404.snowygui.Snowy
import fr.username404.snowygui.gui.feature.ButtonImpl
import fr.username404.snowygui.gui.feature.ButtonInfo
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback.EVENT
import org.reflections8.Reflections
import org.reflections8.scanners.SubTypesScanner
import org.reflections8.scanners.TypeAnnotationsScanner
import org.reflections8.util.ClasspathHelper
import org.reflections8.util.ConfigurationBuilder
import net.fabricmc.loader.api.FabricLoader
import java.net.URLClassLoader
import kotlin.io.path.exists
import kotlin.io.path.isDirectory
import kotlin.io.path.listDirectoryEntries
import kotlin.io.path.pathString
class FabricInit: Snowy(), ClientModInitializer {
override fun onInitializeClient() {
@ -25,11 +25,19 @@ class FabricInit: Snowy(), ClientModInitializer {
)
}
init {
annotatedButtons = Reflections(
ConfigurationBuilder()
.setUrls(ClasspathHelper.forPackage(FeaturePackage))
.setScanners(SubTypesScanner(false), TypeAnnotationsScanner())
.useParallelExecutor()
).getTypesAnnotatedWith(ButtonInfo::class.java).filter { it.isValidForButtonCollection() }.map { it.asSubclass(ButtonImpl::class.java) }.toSet()
val modsPaths = FabricLoader.getInstance().allMods.map {
it.getPath(FeaturePackage.replace('.', '/'))
}.filter { it.exists() && it.isDirectory() }
val buttonsLoader = URLClassLoader(modsPaths.map { it.toUri().toURL() }.toTypedArray(), Thread.currentThread().contextClassLoader)
annotatedButtons = modsPaths.flatMap { buttonsDirectory ->
val classSet = mutableSetOf<Class<out ButtonImpl>>()
buttonsDirectory.run {
listDirectoryEntries("*.class").forEach { file ->
val foundClass = buttonsLoader.loadClass(file.pathString.drop(1).replace('/', '.').removeSuffix(".class"))
if (foundClass.isValidForButtonCollection()) classSet.add(foundClass.asSubclass(ButtonImpl::class.java))
}
}
classSet
}.toSet()
}
}