aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/misc/LicenseViewer.kt
blob: 4219177186e3a1ddb5c2d8790819fd271d772349 (plain)
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
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()
			}
		}
	}
}