From 1eb63c90589b7d7506bffa7ec237063ef27d2ec5 Mon Sep 17 00:00:00 2001 From: Liplum Date: Thu, 7 Dec 2023 02:25:17 +0800 Subject: [PATCH] migrate welcome info list from json to kotlin --- main/build.gradle.kts | 4 -- main/src/net/liplum/welcome/Scenes.kt | 45 +++++++++++++++ main/src/net/liplum/welcome/Welcome.kt | 68 ++++------------------- main/src/net/liplum/welcome/WelcomeTip.kt | 27 +++++++++ meta/WelcomeInfo.json | 65 ---------------------- 5 files changed, 83 insertions(+), 126 deletions(-) create mode 100644 main/src/net/liplum/welcome/Scenes.kt delete mode 100644 meta/WelcomeInfo.json diff --git a/main/build.gradle.kts b/main/build.gradle.kts index cbb0b164..9d46bc14 100644 --- a/main/build.gradle.kts +++ b/main/build.gradle.kts @@ -105,10 +105,6 @@ tasks.jar { archiveBaseName.set("CyberIO") includeEmptyDirs = false exclude("**/**/*.java") - - from("$rootDir/meta") { - include("*.json") - } } java { diff --git a/main/src/net/liplum/welcome/Scenes.kt b/main/src/net/liplum/welcome/Scenes.kt new file mode 100644 index 00000000..fd97ef2d --- /dev/null +++ b/main/src/net/liplum/welcome/Scenes.kt @@ -0,0 +1,45 @@ +package net.liplum.welcome + +object Scenes { + val common = WelcomeTipPack( + scenes = listOf( + "UpdateECHO", + "TheSHIP", + "ECHO", + "NewBrains", + "Discord", + "AdBlock", + "404NotFound", + "OhNo", + "RateUs", + "SetOutErekir", + "CyberionForErekir" + ) + ) + val v2 = WelcomeTipPack( + default = "EmploymentVerification", + scenes = listOf( + "EmploymentVerification" + ) + ) + val v3 = common.inherit( + default = "ResearchProject", + scenes = listOf( + "EmploymentVerification" + ) + ) + val v4 = common.inherit( + default = "ErekirDetected", + scenes = listOf( + "ErekirDetected", + "SetOutErekir" + ) + ) + val v4_1 = v4.inherit( + scenes = listOf( + "CyberionForErekir" + ) + ) + val v5 = v4_1.inherit() + val v5_1 = v5.inherit() +} \ No newline at end of file diff --git a/main/src/net/liplum/welcome/Welcome.kt b/main/src/net/liplum/welcome/Welcome.kt index ef2c0039..8aa01a0e 100644 --- a/main/src/net/liplum/welcome/Welcome.kt +++ b/main/src/net/liplum/welcome/Welcome.kt @@ -2,12 +2,8 @@ package net.liplum.welcome import arc.Core import arc.Events -import arc.struct.ObjectMap -import arc.util.ArcRuntimeException import arc.util.Time -import arc.util.serialization.JsonValue import mindustry.game.EventType.Trigger -import mindustry.io.JsonIO import net.liplum.CioMod import net.liplum.Meta import net.liplum.Settings.CioVersion @@ -20,7 +16,6 @@ import net.liplum.annotations.Only import net.liplum.annotations.SubscribeEvent import net.liplum.blocks.tmtrainer.RandomName import net.liplum.cio -import net.liplum.common.Res import net.liplum.common.util.ReferBundleWrapper import net.liplum.common.util.allMaxBy import net.liplum.common.util.randomExcept @@ -33,10 +28,11 @@ import plumy.dsl.sprite @ClientOnly object Welcome { var bundle = ReferBundleWrapper.create() - private var info = Info() - fun genEntity() = Entity(bundle, info) + private var version = Scenes.v5_1 + fun genEntity() = Entity(bundle, version) private var entity = genEntity() private var showWelcome = false + @JvmStatic @ClientOnly fun showWelcomeDialog() { @@ -50,9 +46,10 @@ object Welcome { tip?.condition?.canShow(tip)*/ //entity.showTipByID("SetOutErekir") } + @JvmStatic fun judgeWelcome() { - val allTips = info.scenes.map { Welcomes[it] }.distinct().toList() + val allTips = version.scenes.map { Welcomes[it] }.distinct().toList() val tipsCanShow = allTips.filter { it.condition.canShow(it) } val allCandidates = tipsCanShow.allMaxBy { it.condition.priority(it) } if (allCandidates.isEmpty()) { @@ -79,6 +76,7 @@ object Welcome { showWelcome = true } } + @JvmStatic @SubscribeEvent(CioInitEvent::class, Only.client) fun modifierModInfo() { @@ -90,6 +88,7 @@ object Welcome { } } } + @JvmStatic fun checkLastVersion() { val lastVersion = CioVersion @@ -101,20 +100,22 @@ object Welcome { } CioVersion = Meta.Version } + @JvmStatic fun recordClick() { ClickWelcomeTimes += 1 } + @JvmStatic @ClientOnly fun load() { loadBundle() - loadInfo() //To load all templates and actions Templates Actions Conditions } + @JvmStatic fun loadBundle() { bundle.loadMoreFrom("welcomes") @@ -123,53 +124,6 @@ object Welcome { } } - lateinit var infoJson: ObjectMap - @Suppress("UNCHECKED_CAST") - @JvmStatic - fun loadInfo() { - val json = Res("WelcomeInfo.json").readAllText() - infoJson = JsonIO.json.fromJson(ObjectMap::class.java, json) as ObjectMap - val curInfo = infoJson.get(Meta.Version) - ?: throw ArcRuntimeException("The welcome message information of Cyber IO ${Meta.Version} not found.") - val default = curInfo.get("Default")?.asString() ?: "Default" - val scenes = curInfo.get("Scene")?.asStringArray() ?: emptyArray() - val parent: String? = curInfo.get("Parent")?.asString() - info.default = default - val allScenes = HashSet() - allScenes.addAll(scenes) - fun loadParent(parent: String) { - val parentInfo = infoJson.get(parent) - val parentScenes = parentInfo.get("Scene").asStringArray() - val parentParent: String? = parentInfo.get("Parent")?.asString() - allScenes.addAll(parentScenes) - if (parentParent != null) - loadParent(parentParent) - } - if (parent != null) { - loadParent(parent) - } - info.scenes = allScenes.toList() - } - - class Info { - var default = "Default" - var scenes: List = emptyList() - val sceneSize: Int - get() = scenes.size - - operator fun get(index: Int) = - if (index !in scenes.indices) - default - else - scenes[index] - - fun indexOf(tipID: String): Int = - scenes.indexOf(tipID) - - val defaultIndex: Int - get() = indexOf(default) - } - fun String.handleTrRefer(): TR = if (startsWith('@')) removePrefix("@").sprite @@ -177,7 +131,7 @@ object Welcome { class Entity( val bundle: ReferBundleWrapper, - val info: Info, + val info: WelcomeTipPack, ) { var tip: WelcomeTip = WelcomeTip.Default operator fun get(key: String) = diff --git a/main/src/net/liplum/welcome/WelcomeTip.kt b/main/src/net/liplum/welcome/WelcomeTip.kt index d221b591..9ee06325 100644 --- a/main/src/net/liplum/welcome/WelcomeTip.kt +++ b/main/src/net/liplum/welcome/WelcomeTip.kt @@ -23,3 +23,30 @@ class WelcomeTip( const val DefaultIconPath = "icon" } } + +class WelcomeTipPack( + var default: String = "Default", + var scenes: List = emptyList(), +) { + operator fun get(index: Int) = + if (index !in scenes.indices) + default + else + scenes[index] + + fun indexOf(tipID: String): Int = + scenes.indexOf(tipID) + + val defaultIndex: Int + get() = indexOf(default) + + fun inherit( + default: String? = null, + scenes: List = emptyList(), + ): WelcomeTipPack { + return WelcomeTipPack( + default = default ?: this.default, + scenes = this.scenes + scenes, + ) + } +} \ No newline at end of file diff --git a/meta/WelcomeInfo.json b/meta/WelcomeInfo.json deleted file mode 100644 index 2399aa7d..00000000 --- a/meta/WelcomeInfo.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "common": { - "Scene": [ - "UpdateECHO", - "TheSHIP", - "ECHO", - "NewBrains", - "Discord", - "AdBlock", - "404NotFound", - "OhNo", - "RateUs", - "SetOutErekir", - "CyberionForErekir" - ] - }, - "common-erekir": { - "Parent": "common", - "Scene": [ - "SetOutErekir", - "CyberionForErekir" - ] - }, - "v0": {}, - "v2": { - "Default": "EmploymentVerification", - "Scene": [ - "EmploymentVerification" - ] - }, - "v3": { - "Default": "ResearchProject", - "Parent": "common", - "Scene": [ - "ResearchProject" - ] - }, - "v4": { - "Default": "ErekirDetected", - "Parent": "common", - "Scene": [ - "ErekirDetected", - "SetOutErekir" - ] - }, - "v4.1": { - "Default": "ErekirDetected", - "Parent": "v4", - "Scene": [ - "CyberionForErekir" - ] - }, - "v5.0": { - "Default": "ErekirDetected", - "Parent": "v4.1", - "Scene": [ - ] - }, - "v5.1": { - "Default": "ErekirDetected", - "Parent": "v4.1", - "Scene": [ - ] - } -} \ No newline at end of file