From 079054d99f8c0c37cf5dec035440e47dd8ebb37b Mon Sep 17 00:00:00 2001 From: cubewhy <61075476+cubewhy@users.noreply.github.com> Date: Sun, 3 Nov 2024 22:26:13 +0800 Subject: [PATCH] feat: hot reloading API --- .../kotlin/org/cubewhy/celestial/Celestial.kt | 5 ++--- .../celestial/event/impl/APIReadyEvent.kt | 11 +++++++++++ .../org/cubewhy/celestial/gui/GuiLauncher.kt | 6 +++--- .../celestial/gui/elements/GuiVersionSelect.kt | 11 +++++++++-- .../org/cubewhy/celestial/gui/pages/GuiNews.kt | 17 +++++++++++++---- .../cubewhy/celestial/gui/pages/GuiSettings.kt | 17 ++++++++++++++++- 6 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 src/main/kotlin/org/cubewhy/celestial/event/impl/APIReadyEvent.kt diff --git a/src/main/kotlin/org/cubewhy/celestial/Celestial.kt b/src/main/kotlin/org/cubewhy/celestial/Celestial.kt index 5d3adc02..2efb46b0 100644 --- a/src/main/kotlin/org/cubewhy/celestial/Celestial.kt +++ b/src/main/kotlin/org/cubewhy/celestial/Celestial.kt @@ -13,6 +13,7 @@ import com.google.gson.Gson import com.google.gson.JsonParser import kotlinx.serialization.json.Json import org.apache.commons.io.FileUtils +import org.cubewhy.celestial.event.impl.APIReadyEvent import org.cubewhy.celestial.event.impl.CreateLauncherEvent import org.cubewhy.celestial.files.DownloadManager import org.cubewhy.celestial.files.Downloadable @@ -55,7 +56,6 @@ val JSON = Json { ignoreUnknownKeys = true; prettyPrint = true } val configDir = File(System.getProperty("user.home"), ".cubewhy/lunarcn") val themesDir = File(configDir, "themes") val configFile = configDir.resolve("celestial.json") -val proxyConfigFile = configDir.resolve("proxy.json") val config: BasicConfig = try { JSON.decodeFromString(configFile.readText()) } catch (e: FileNotFoundException) { @@ -81,7 +81,6 @@ private var sessionFile: File = if (OSEnum.Windows.isCurrent) { File(System.getProperty("user.home"), ".config/launcher/sentry/session.json") } else { // Macos... - // TODO support MACOS // Not tested yet File(System.getProperty("user.home"), "Library/Application Support//launcher/sentry/session.json") } @@ -95,7 +94,6 @@ private lateinit var userLanguage: String var runningOnGui = false var jar = Celestial::class.java.getProtectionDomain().codeSource.location.path.toFile() -var isRunningInJar = jar.isFile val minecraftFolder: File /** @@ -193,6 +191,7 @@ private fun run() { CreateLauncherEvent(launcherFrame).call() launcherFrame.isVisible = true runningOnGui = true + APIReadyEvent().call() launcherFrame.addWindowListener(object : WindowAdapter() { override fun windowClosing(e: WindowEvent) { diff --git a/src/main/kotlin/org/cubewhy/celestial/event/impl/APIReadyEvent.kt b/src/main/kotlin/org/cubewhy/celestial/event/impl/APIReadyEvent.kt new file mode 100644 index 00000000..9fa6b91c --- /dev/null +++ b/src/main/kotlin/org/cubewhy/celestial/event/impl/APIReadyEvent.kt @@ -0,0 +1,11 @@ +/* + * Celestial Launcher + * License under GPLv3 + * Do NOT remove this note if you want to copy this file. + */ + +package org.cubewhy.celestial.event.impl + +import org.cubewhy.celestial.event.Event + +class APIReadyEvent : Event() \ No newline at end of file diff --git a/src/main/kotlin/org/cubewhy/celestial/gui/GuiLauncher.kt b/src/main/kotlin/org/cubewhy/celestial/gui/GuiLauncher.kt index 295d941a..7f1a8572 100644 --- a/src/main/kotlin/org/cubewhy/celestial/gui/GuiLauncher.kt +++ b/src/main/kotlin/org/cubewhy/celestial/gui/GuiLauncher.kt @@ -8,10 +8,11 @@ package org.cubewhy.celestial.gui import com.sun.tools.attach.AttachNotSupportedException import org.cubewhy.celestial.config +import org.cubewhy.celestial.event.EventManager import org.cubewhy.celestial.f import org.cubewhy.celestial.gamePid import org.cubewhy.celestial.metadata -import org.cubewhy.celestial.event.EventManager.register + import org.cubewhy.celestial.event.EventTarget import org.cubewhy.celestial.event.impl.AuthEvent import org.cubewhy.celestial.event.impl.GameStartEvent @@ -37,8 +38,7 @@ class GuiLauncher : JFrame() { lateinit var mainPanel: JPanel init { - // register with EventManager - register(this) + EventManager.register(this) this.setBounds(100, 100, 1200, 700) this.title = f.getString("gui.launcher.title") diff --git a/src/main/kotlin/org/cubewhy/celestial/gui/elements/GuiVersionSelect.kt b/src/main/kotlin/org/cubewhy/celestial/gui/elements/GuiVersionSelect.kt index 93600d12..bf9142a8 100644 --- a/src/main/kotlin/org/cubewhy/celestial/gui/elements/GuiVersionSelect.kt +++ b/src/main/kotlin/org/cubewhy/celestial/gui/elements/GuiVersionSelect.kt @@ -9,6 +9,8 @@ import com.sun.tools.attach.AttachNotSupportedException import org.apache.commons.io.FileUtils import org.cubewhy.celestial.* import org.cubewhy.celestial.event.EventManager +import org.cubewhy.celestial.event.EventTarget +import org.cubewhy.celestial.event.impl.APIReadyEvent import org.cubewhy.celestial.event.impl.GameStartEvent import org.cubewhy.celestial.event.impl.GameTerminateEvent import org.cubewhy.celestial.files.DownloadManager.waitForAll @@ -53,6 +55,8 @@ class GuiVersionSelect : JPanel() { } init { + EventManager.register(this + ) this.border = TitledBorder( null, f.getString("gui.version-select.title"), @@ -62,10 +66,13 @@ class GuiVersionSelect : JPanel() { Color.orange ) this.layout = GridLayout(5, 2, 5, 5) - - this.initGui() } + @EventTarget + fun onAPIReady(e: APIReadyEvent) { + this.removeAll() + initGui() + } private fun initGui() { this.add(JLabel(f.getString("gui.version-select.label.version"))) diff --git a/src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiNews.kt b/src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiNews.kt index 6647a8fb..2fe6d2de 100644 --- a/src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiNews.kt +++ b/src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiNews.kt @@ -1,6 +1,9 @@ package org.cubewhy.celestial.gui.pages import cn.hutool.crypto.SecureUtil +import org.cubewhy.celestial.event.EventManager +import org.cubewhy.celestial.event.EventTarget +import org.cubewhy.celestial.event.impl.APIReadyEvent import org.cubewhy.celestial.f import org.cubewhy.celestial.files.DownloadManager.cache import org.cubewhy.celestial.gui.LauncherBirthday @@ -24,9 +27,10 @@ import kotlin.math.abs class GuiNews : JScrollPane(panel, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED) { - private val blogPosts: List + private lateinit var blogPosts: List init { + EventManager.register(this) this.border = TitledBorder( null, f.getString("gui.news.title"), @@ -36,8 +40,7 @@ class GuiNews : JScrollPane(panel, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCRO Color.orange ) panel.layout = BoxLayout(panel, BoxLayout.Y_AXIS) - blogPosts = metadata.blogposts - this.initGui() + getVerticalScrollBar().unitIncrement = 30 } @@ -48,9 +51,15 @@ class GuiNews : JScrollPane(panel, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCRO return ChronoUnit.DAYS.between(today, birthday).toInt() } + @EventTarget + fun onAPIReady(event: APIReadyEvent) { + blogPosts = metadata.blogposts + panel.removeAll() + initGui() + } + private fun initGui() { // render blogPosts - getVerticalScrollBar().unitIncrement = 30 log.info("Loading blogPosts (gui)") val birthday = calcBirthday() if (abs(birthday) <= 10) { diff --git a/src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiSettings.kt b/src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiSettings.kt index 1da6c01b..1d5a8b6c 100644 --- a/src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiSettings.kt +++ b/src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiSettings.kt @@ -9,6 +9,8 @@ package org.cubewhy.celestial.gui.pages import org.apache.commons.io.FileUtils import org.cubewhy.celestial.* import org.cubewhy.celestial.event.EventManager +import org.cubewhy.celestial.event.EventTarget +import org.cubewhy.celestial.event.impl.APIReadyEvent import org.cubewhy.celestial.event.impl.ChangeConfigEvent import org.cubewhy.celestial.game.addon.LunarCNMod import org.cubewhy.celestial.game.addon.WeaveMod @@ -20,6 +22,7 @@ import org.cubewhy.celestial.gui.dialogs.MirrorDialog import org.cubewhy.celestial.gui.layouts.VerticalFlowLayout import org.cubewhy.celestial.utils.* import org.cubewhy.celestial.utils.OSEnum.Companion.current +import org.cubewhy.celestial.utils.lunar.LauncherData import org.slf4j.Logger import org.slf4j.LoggerFactory import java.awt.Color @@ -54,6 +57,19 @@ class GuiSettings : JScrollPane(panel, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_ this.initGui() } + /** + * Hot reload config + * */ + @EventTarget + fun onChangeConfig(e: ChangeConfigEvent<*>) { + if (e.configObject is BasicConfig && e.key == "api") { + log.info("API changed, hot reloading...") + launcherData = LauncherData(e.newValue as String) + metadata = launcherData.metadata() + APIReadyEvent().call() + } + } + private fun initGui() { // config panel.add(JLabel(f.getString("gui.settings.warn.restart"))) @@ -575,7 +591,6 @@ class GuiSettings : JScrollPane(panel, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_ return cb } - companion object { private val panel = JPanel() private val log: Logger = LoggerFactory.getLogger(GuiSettings::class.java)