Skip to content

Commit

Permalink
feat: hot reloading API
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Nov 3, 2024
1 parent 11411c0 commit 079054d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/main/kotlin/org/cubewhy/celestial/Celestial.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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")
}
Expand All @@ -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
/**
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/org/cubewhy/celestial/event/impl/APIReadyEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Celestial Launcher <me@lunarclient.top>
* 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()
6 changes: 3 additions & 3 deletions src/main/kotlin/org/cubewhy/celestial/gui/GuiLauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +55,8 @@ class GuiVersionSelect : JPanel() {
}

init {
EventManager.register(this
)
this.border = TitledBorder(
null,
f.getString("gui.version-select.title"),
Expand All @@ -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")))
Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiNews.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,9 +27,10 @@ import kotlin.math.abs


class GuiNews : JScrollPane(panel, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED) {
private val blogPosts: List<Blogpost>
private lateinit var blogPosts: List<Blogpost>

init {
EventManager.register(this)
this.border = TitledBorder(
null,
f.getString("gui.news.title"),
Expand All @@ -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
}


Expand All @@ -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) {
Expand Down
17 changes: 16 additions & 1 deletion src/main/kotlin/org/cubewhy/celestial/gui/pages/GuiSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 079054d

Please sign in to comment.