feat: Add license viewer /firm licenses

This commit is contained in:
Linnea Gräf
2025-05-25 22:18:33 +02:00
parent 03064dd01f
commit 8f82b1e6bf
6 changed files with 264 additions and 46 deletions

View File

@@ -0,0 +1,128 @@
package moe.nea.firmament.features.misc
import io.github.notenoughupdates.moulconfig.observer.ObservableList
import io.github.notenoughupdates.moulconfig.xml.Bind
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.json.decodeFromStream
import moe.nea.firmament.Firmament
import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.commands.thenExecute
import moe.nea.firmament.events.CommandEvent
import moe.nea.firmament.util.ErrorUtil
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.MoulConfigUtils
import moe.nea.firmament.util.ScreenUtil
import moe.nea.firmament.util.tr
object LicenseViewer {
@Serializable
data class Software(
val licenses: List<License> = listOf(),
val webPresence: String? = null,
val projectName: String,
val projectDescription: String? = null,
val developers: List<Developer> = listOf(),
) {
@Bind
fun hasWebPresence() = webPresence != null
@Bind
fun webPresence() = webPresence ?: "<no web presence>"
@Bind
fun open() {
MC.openUrl(webPresence ?: return)
}
@Bind
fun projectName() = projectName
@Bind
fun projectDescription() = projectDescription ?: "<no project description>"
@get:Bind("developers")
@Transient
val developersO = ObservableList(developers)
@get:Bind("licenses")
@Transient
val licenses0 = ObservableList(licenses)
}
@Serializable
data class Developer(
@get:Bind("name") val name: String,
val webPresence: String? = null
) {
@Bind
fun open() {
MC.openUrl(webPresence ?: return)
}
@Bind
fun hasWebPresence() = webPresence != null
@Bind
fun webPresence() = webPresence ?: "<no web presence>"
}
@Serializable
data class License(
@get:Bind("name") val licenseName: String,
val licenseUrl: String? = null
) {
@Bind
fun open() {
MC.openUrl(licenseUrl ?: return)
}
@Bind
fun hasUrl() = licenseUrl != null
@Bind
fun url() = licenseUrl ?: "<no link to license text>"
}
data class LicenseList(
val softwares: List<Software>
) {
@get:Bind("softwares")
val obs = ObservableList(softwares)
}
@OptIn(ExperimentalSerializationApi::class)
val licenses: LicenseList? = ErrorUtil.catch("Could not load licenses") {
Firmament.json.decodeFromStream<List<Software>?>(
javaClass.getResourceAsStream("/LICENSES-FIRMAMENT.json") ?: error("Could not find LICENSES-FIRMAMENT.json")
)?.let { LicenseList(it) }
}.orNull()
fun showLicenses() {
ErrorUtil.catch("Could not display licenses") {
ScreenUtil.setScreenLater(
MoulConfigUtils.loadScreen(
"license_viewer/index", licenses!!, null
)
)
}.or {
MC.sendChat(
tr(
"firmament.licenses.notfound",
"Could not load licenses. Please check the Firmament source code for information directly."
)
)
}
}
@Subscribe
fun onSubcommand(event: CommandEvent.SubCommand) {
event.subcommand("licenses") {
thenExecute {
showLicenses()
}
}
}
}

View File

@@ -38,6 +38,8 @@ object ErrorUtil {
}
class Catch<T> private constructor(val value: T?, val exc: Throwable?) {
fun orNull(): T? = value
inline fun or(block: (exc: Throwable) -> T): T {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)

View File

@@ -21,10 +21,10 @@ import net.minecraft.registry.Registry
import net.minecraft.registry.RegistryKey
import net.minecraft.registry.RegistryKeys
import net.minecraft.registry.RegistryWrapper
import net.minecraft.registry.entry.RegistryEntry
import net.minecraft.resource.ReloadableResourceManagerImpl
import net.minecraft.text.Text
import net.minecraft.util.Identifier
import net.minecraft.util.Util
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import moe.nea.firmament.events.TickEvent
@@ -127,6 +127,10 @@ object MC {
private set
fun openUrl(uri: String) {
Util.getOperatingSystem().open(uri)
}
fun <T> unsafeGetRegistryEntry(registry: RegistryKey<out Registry<T>>, identifier: Identifier) =
unsafeGetRegistryEntry(RegistryKey.of(registry, identifier))

View File

@@ -37,6 +37,19 @@ import moe.nea.firmament.gui.TickComponent
import moe.nea.firmament.util.render.isUntranslatedGuiDrawContext
object MoulConfigUtils {
@JvmStatic
fun main(args: Array<out String>) {
generateXSD(File("MoulConfig.xsd"), XMLUniverse.MOULCONFIG_XML_NS)
generateXSD(File("MoulConfig.Firmament.xsd"), firmUrl)
File("wrapper.xsd").writeText("""
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://notenoughupdates.org/moulconfig" schemaLocation="MoulConfig.xsd"/>
<xs:import namespace="http://firmament.nea.moe/moulconfig" schemaLocation="MoulConfig.Firmament.xsd"/>
</xs:schema>
""".trimIndent())
}
val firmUrl = "http://firmament.nea.moe/moulconfig"
val universe = XMLUniverse.getDefaultUniverse().also { uni ->
uni.registerMapper(java.awt.Color::class.java) {
@@ -181,10 +194,8 @@ object MoulConfigUtils {
uni.registerLoader(object : XMLGuiLoader.Basic<FixedComponent> {
override fun createInstance(context: XMLContext<*>, element: Element): FixedComponent {
return FixedComponent(
context.getPropertyFromAttribute(element, QName("width"), Int::class.java)
?: error("Requires width specified"),
context.getPropertyFromAttribute(element, QName("height"), Int::class.java)
?: error("Requires height specified"),
context.getPropertyFromAttribute(element, QName("width"), Int::class.java),
context.getPropertyFromAttribute(element, QName("height"), Int::class.java),
context.getChildFragment(element)
)
}
@@ -198,7 +209,7 @@ object MoulConfigUtils {
}
override fun getAttributeNames(): Map<String, Boolean> {
return mapOf("width" to true, "height" to true)
return mapOf("width" to false, "height" to false)
}
})
}
@@ -212,19 +223,6 @@ object MoulConfigUtils {
generator.dumpToFile(file)
}
@JvmStatic
fun main(args: Array<out String>) {
generateXSD(File("MoulConfig.xsd"), XMLUniverse.MOULCONFIG_XML_NS)
generateXSD(File("MoulConfig.Firmament.xsd"), firmUrl)
File("wrapper.xsd").writeText("""
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://notenoughupdates.org/moulconfig" schemaLocation="MoulConfig.xsd"/>
<xs:import namespace="http://firmament.nea.moe/moulconfig" schemaLocation="MoulConfig.Firmament.xsd"/>
</xs:schema>
""".trimIndent())
}
fun loadScreen(name: String, bindTo: Any, parent: Screen?): Screen {
return object : GuiComponentWrapper(loadGui(name, bindTo)) {
override fun close() {