Skip to content

Commit

Permalink
Experimental | Optimize Settings
Browse files Browse the repository at this point in the history
Allowed to get settings in any LifeCycle
  • Loading branch information
TheFloodDragon committed Jul 26, 2024
1 parent 5587e3b commit fe6a9bb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ val adventureModules = setOf(
* 要合并的文件列表
*/
val combineFiles = setOf(
"config.yml",
"settings.yml",
"lang/zh_CN.yml",
"lang/en_US.yml"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
package cn.fd.ratziel.common.config

import taboolib.common.platform.function.getDataFolder
import taboolib.module.configuration.Config
import taboolib.module.configuration.ConfigNode
import taboolib.common.platform.function.releaseResourceFile
import taboolib.common.util.asList
import taboolib.module.configuration.Configuration

object Settings {

private const val FILE = "settings.yml"

@Config(FILE)
lateinit var conf: Configuration internal set

@ConfigNode("Workspaces.listen", FILE)
var listenFiles = true

@ConfigNode("Workspaces.paths", FILE)
var workspacePaths = listOf("${getDataFolder()}/workspace")

@ConfigNode("Workspaces.filter", FILE)
var fileFilter = "^(?![#!]).*\\.(?i)(yaml|yml|toml|tml|json|conf)\$"
/**
* 配置文件路径
*/
val file by lazy { releaseResourceFile(source = "settings.yml", target = "settings.yml") }

/**
* 配置文件
*/
lateinit var conf: Configuration
internal set

/**
* 重载配置文件
*/
fun reloadConfig() {
conf = Configuration.loadFromFile(file)
}

init {
reloadConfig()
}

/**
* 配置项 - 工作空间路径列表
*/
val workspacePaths get() = conf["Workspaces.paths"]?.asList() ?: listOf("${getDataFolder()}/workspace")

/**
* 配置项 - 工作空间文件过滤器 (正则表达式)
*/
var fileFilter = conf["Workspaces.filter"]?.toString() ?: "^(?![#!]).*\\.(?i)(yaml|yml|toml|tml|json|conf)\$"

/**
* 配置项 - 是否启用文件监听
*/
val listenFiles get() = conf["Workspaces.listen"]?.toString()?.toBooleanStrictOrNull() ?: true

}
2 changes: 1 addition & 1 deletion project/module-common/src/main/resources/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Workspaces:
listen: true # 若开启则会监听工作空间内的文件变化,并自动重新加载变化文件
filter: '^(?![#!]).*\.(?i)(yaml|yml|toml|tml|json|conf)$'
paths:
- 'plugins/Ratziel/workspace'
- 'plugins/Ratziel/workspace'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cn.fd.ratziel.script
import cn.fd.ratziel.common.config.Settings
import cn.fd.ratziel.script.api.ScriptType
import taboolib.common.ClassAppender
import taboolib.common.LifeCycle
import taboolib.common.env.JarRelocation
import taboolib.common.env.RuntimeEnv
import taboolib.common.platform.Awake
Expand All @@ -24,14 +23,14 @@ object ScriptManager {
var defaultLanguage: ScriptType = ScriptTypes.KETHER
internal set

@Awake(LifeCycle.INIT)
@Awake
private fun init() {
// Read config
val conf = Settings.conf.getConfigurationSection("Script")

// Default Language
val defLanguage = conf?.getString("Default")?.let { ScriptTypes.match(it) }
if (defLanguage != null) defaultLanguage = defLanguage
val defLang = conf?.getString("Default")?.let { ScriptTypes.match(it) }
if (defLang != null) defaultLanguage = defLang

// Options
val optionsConf = conf?.getConfigurationSection("Options")
Expand Down
6 changes: 3 additions & 3 deletions project/module-script/src/main/resources/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Script:
Default: Kether # 默认使用的脚本引擎
Options:
JavaScript:
enabled: true
Kether:
enabled: true
JavaScript:
enabled: true
Jexl:
enabled: true
Kotlin:
enabled: true
enabled: true

0 comments on commit fe6a9bb

Please sign in to comment.