Skip to content

Commit e6bda40

Browse files
committed
⬆️ ScriptAgent 2.0 & Kotlin K2
1 parent 51537b2 commit e6bda40

15 files changed

Lines changed: 171 additions & 92 deletions

File tree

build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import org.apache.tools.ant.filters.ReplaceTokens
12
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
23

34
plugins {
4-
kotlin("jvm") version "2.1.10"
5+
kotlin("jvm") version "2.3.0"
56
id("me.qoomon.git-versioning") version "6.4.4"
67
id("com.gradleup.shadow") version "8.3.6"
78
}
@@ -78,7 +79,7 @@ dependencies.constraints {
7879
api("dev.folia:folia-api:$bukkitVersion")
7980
}
8081
dependencies {
81-
val libraryVersion = "1.11.4.1"
82+
val libraryVersion = "2.0.0"
8283
api("cf.wayzer:ScriptAgent:${libraryVersion}")
8384
implementation("cf.wayzer:LibraryManager:1.6")
8485

buildSrc/src/Module.kt

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,74 @@
11
@file:Suppress("unused")
22

33
import org.gradle.api.Project
4+
import org.gradle.api.artifacts.Dependency
45
import org.gradle.api.artifacts.dsl.DependencyHandler
56
import org.gradle.api.tasks.SourceSet
67
import org.gradle.api.tasks.SourceSetContainer
8+
import org.gradle.kotlin.dsl.dependencies
79
import org.gradle.kotlin.dsl.kotlin
810
import org.gradle.kotlin.dsl.project
911

10-
class ModuleScope(val moduleId: String, private val project: Project, private val sourceSet: SourceSet) {
11-
fun DependencyHandler.dependsModule(module: String, project: Project = this@ModuleScope.project) =
12-
add(sourceSet.apiConfigurationName, project(project.path, module + "Exposed"))
12+
@DslMarker
13+
annotation class ModuleBuilderMarker
14+
15+
@ModuleBuilderMarker
16+
class ModuleScope(val moduleId: String, val project: Project, val sourceSet: SourceSet) {
17+
val exposedConfigurationName get() = sourceSet.apiConfigurationName.replace("Api", "Exposed")
18+
19+
fun DependencyHandler.dependsOnModule(module: String, project: Project = this@ModuleScope.project): Dependency? {
20+
val sourceSet = project.sourceSets.getByName(mapIdToSourceSetName(module))
21+
return dependsOn(ModuleScope(module, project, sourceSet))
22+
}
23+
24+
fun DependencyHandler.dependsOn(module: ModuleScope): Dependency? {
25+
return add(sourceSet.apiConfigurationName, project(module.project.path, module.exposedConfigurationName))
26+
}
1327

1428
fun DependencyHandler.api(dep: Any) = add(sourceSet.apiConfigurationName, dep)
1529
fun DependencyHandler.implementation(dep: Any) = add(sourceSet.implementationConfigurationName, dep)
1630
}
1731

32+
private fun mapIdToSourceSetName(id: String) = id.replace('/', '.')
33+
34+
@ModuleBuilderMarker
1835
fun Project.defineModule(
1936
name: String,
20-
srcDir: String = name,
2137
body: ModuleScope.() -> Unit,
2238
) {
23-
val sourceSet = sourceSets.create(name) {
24-
java.srcDir(srcDir)
25-
}
26-
val exposed = configurations.create(name + "Exposed") {
27-
extendsFrom(configurations.getByName(name + "Api"))
28-
isCanBeConsumed = true
29-
}
30-
dependencies.apply {
31-
add(exposed.name, sourceSet.output)
39+
val sourceSet = sourceSets.create(mapIdToSourceSetName(name)) {
40+
java.srcDir(name)
3241
}
3342
ModuleScope(name, project, sourceSet).apply {
43+
configurations.create(exposedConfigurationName) {
44+
extendsFrom(configurations.getByName(sourceSet.apiConfigurationName))
45+
isCanBeConsumed = true
46+
isCanBeResolved = false
47+
}
3448
dependencies.apply {
3549
implementation(kotlin("script-runtime"))
3650
implementation(rootProject)
51+
add(exposedConfigurationName, sourceSet.output)
3752
}
3853
body()
3954
}
4055
}
4156

57+
@ModuleBuilderMarker
58+
fun ModuleScope.subModule(name: String, body: ModuleScope.() -> Unit) {
59+
val parent = this@subModule
60+
val project = parent.project
61+
62+
val id = "${parent.moduleId}/$name"
63+
parent.sourceSet.java.exclude("$name/**")
64+
project.dependencies {
65+
project.defineModule(id) {
66+
dependsOn(parent)
67+
body()
68+
}
69+
}
70+
}
71+
4272
private val Project.sourceSets
4373
get() = project.extensions.getByType(
4474
SourceSetContainer::class.java

loader/mindustry/res/META-INF/kotlin/script/templates/.gitkeep

Whitespace-only changes.

scripts/bootStrap/default.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ suspend fun boot() = ScriptManager.transaction {
77

88
//add 添加需要加载的脚本(前缀判断) exclude 排除脚本(可以作为依赖被加载)
99
addAll()
10+
exclude("@")
1011
exclude("bootStrap/")
1112
exclude("coreLibrary/extApi/")//lazy load
1213
exclude("scratch")

scripts/build.gradle.kts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,59 @@ plugins {
44
kotlin("jvm")
55
}
66
dependencies {
7-
defineModule("kcp") {}
7+
defineModule("kcp") {
8+
//kcp/serialization
9+
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
10+
}
811
defineModule("bootStrap") {}
912
defineModule("coreLibrary") {
1013
api("com.github.way-zer:PlaceHoldLib:v7.3")
1114
api("io.github.config4k:config4k:0.7.0")
1215
api("org.slf4j:slf4j-api:2.0.16")
13-
//coreLib/kcp/serialization
14-
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
15-
//coreLib/DBApi
16-
val exposedVersion = "0.59.0"
17-
api("org.jetbrains.exposed:exposed-core:$exposedVersion")
18-
api("org.jetbrains.exposed:exposed-dao:$exposedVersion")
19-
api("org.jetbrains.exposed:exposed-java-time:$exposedVersion")
20-
//coreLib/extApi/redisApi
21-
api("redis.clients:jedis:4.3.1")
22-
//coreLib/extApi/mongoApi
23-
api("org.litote.kmongo:kmongo-coroutine:4.8.0")
24-
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
25-
//coreLib/extApi/KVStore
26-
api("com.h2database:h2-mvstore:2.3.232")
16+
17+
subModule("db"){
18+
val exposedVersion = "0.59.0"
19+
api("org.jetbrains.exposed:exposed-core:$exposedVersion")
20+
api("org.jetbrains.exposed:exposed-dao:$exposedVersion")
21+
api("org.jetbrains.exposed:exposed-java-time:$exposedVersion")
22+
}
23+
24+
subModule("extApi"){
25+
//coreLib/extApi/redisApi
26+
api("redis.clients:jedis:4.3.1")
27+
//coreLib/extApi/mongoApi
28+
api("org.litote.kmongo:kmongo-coroutine:4.8.0")
29+
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
30+
//coreLib/extApi/KVStore
31+
api("com.h2database:h2-mvstore:2.3.232")
32+
}
2733
}
2834

2935
defineModule("javalin") {
30-
dependsModule("coreLibrary")
36+
dependsOnModule("coreLibrary")
3137
api("io.javalin:javalin:6.7.0")
3238
}
3339

3440
defineModule("coreMindustry") {
35-
dependsModule("coreLibrary")
41+
dependsOnModule("coreLibrary")
3642
api("com.github.TinyLake.MindustryX:core")
3743
//coreMindustry/console
3844
implementation("org.jline:jline-terminal:3.21.0")
3945
implementation("org.jline:jline-reader:3.21.0")
4046
}
4147
defineModule("scratch") {
42-
dependsModule("coreLibrary")
48+
dependsOnModule("coreLibrary")
4349
}
4450

4551
defineModule("wayzer") {
46-
dependsModule("coreMindustry")
52+
dependsOnModule("coreMindustry")
53+
dependsOnModule("coreLibrary/db")
4754
api("com.google.guava:guava:30.1-jre")
4855
//wayzer/ext/profiler
4956
implementation("tools.profiler:async-profiler:4.1")
5057
}
5158
defineModule("mapScript") {
52-
dependsModule("wayzer")
59+
dependsOnModule("wayzer")
5360
}
5461
}
5562

@@ -60,7 +67,7 @@ allprojects {
6067
"-Xinline-classes",
6168
"-opt-in=kotlin.RequiresOptIn",
6269
"-Xnullability-annotations=@arc.util:strict",
63-
"-Xcontext-receivers",
70+
"-Xcontext-parameters",
6471
)
6572
}
6673
}
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
package coreLibrary.extApi
1+
package coreLib.extApi
22

33
import cf.wayzer.scriptAgent.Event
4-
import cf.wayzer.scriptAgent.contextScript
4+
import cf.wayzer.scriptAgent.define.SAExperimentalApi
5+
import cf.wayzer.scriptAgent.util.Services
6+
import coreLibrary.lib.all
57
import java.io.Serializable
6-
import java.lang.ref.WeakReference
78

89
@Suppress("unused")//Api
910
abstract class RemoteEvent : Event, Serializable {
1011
private val handler0 get() = super.handler
1112
final override val handler: Event.Handler get() = error("You should use RemoteEvent.emit()")
1213

1314
fun launchEmit() {
14-
Impl.script.remoteEmit(this)
15+
service.forEach {
16+
it.remoteEmit(this)
17+
}
1518
}
1619

1720
internal suspend fun onReceive() {
@@ -21,12 +24,18 @@ abstract class RemoteEvent : Event, Serializable {
2124
abstract class Handler : Event.Handler() {
2225
init {
2326
val eventCls = javaClass.enclosingClass
24-
Impl.classMap[eventCls.name] = WeakReference(eventCls)
27+
service.forEach {
28+
it.registerType(eventCls)
29+
}
2530
}
2631
}
2732

28-
internal object Impl {
29-
val script = contextScript<RemoteEventApi>()
30-
val classMap = mutableMapOf<String, WeakReference<Class<*>>>()
33+
interface Impl {
34+
fun remoteEmit(event: RemoteEvent)
35+
fun registerType(cls: Class<*>)
36+
}
37+
@OptIn(SAExperimentalApi::class)
38+
companion object {
39+
val service: List<Impl> by Services.get<Impl>().all
3140
}
3241
}

scripts/coreLibrary/extApi/remoteEventApi.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
@file:Depends("coreLibrary/extApi/redisApi", "基于redis")
1+
@file:Depends("coreLib/extApi/redisApi", "基于redis")
2+
@file:Import("./lib/RemoteEvent.kt")
23

34
package coreLibrary.extApi
45

@@ -7,6 +8,7 @@ import java.io.*
78
import java.util.logging.Level
89

910
val group by config.key("_SA_RemoteEvent")
11+
val classMap = mutableMapOf<String, WeakReference<Class<*>>>()
1012

1113
fun remoteEmit(event: RemoteEvent) = launch(Dispatchers.IO) {
1214
RedisApi.Redis.use {
@@ -17,6 +19,10 @@ fun remoteEmit(event: RemoteEvent) = launch(Dispatchers.IO) {
1719
}
1820
}
1921

22+
fun registerType(cls: Class<*>) {
23+
classMap[eventCls.name] = WeakReference(eventCls)
24+
}
25+
2026
fun handleReceive(msg: ByteArray) {
2127
try {
2228
val event = object : ObjectInputStream(ByteArrayInputStream(msg)) {

scripts/coreLibrary/lib/CommandApi.kt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ typealias CommandHandlerOld = suspend CommandContext.() -> Unit
110110

111111
fun interface CommandHandler {
112112
//Default only handle Command
113-
context(CommandContext) fun canHandle() = context is CommandContext.Command
113+
fun CommandContext.canHandle() = context is CommandContext.Command
114114

115-
context(CommandContext) suspend fun handle()
115+
suspend fun CommandContext.handle()
116116
}
117117

118118
@Deprecated("use CommandHandler.canHandle logic")
@@ -192,7 +192,7 @@ class CommandInfo(
192192
freeze()
193193
}
194194

195-
context(CommandContext) override fun canHandle(): Boolean =
195+
override fun CommandContext.canHandle(): Boolean =
196196
context is CommandContext.TabComplete || body.canHandle()
197197

198198
override suspend fun onComplete(context: CommandContext) {
@@ -212,19 +212,19 @@ class CommandInfo(
212212
(body as? TabCompleter)?.onComplete(context)
213213
}
214214

215-
context(CommandContext) override suspend fun handle() {
215+
override suspend fun CommandContext.handle() {
216216
if (context is CommandContext.TabComplete)
217217
return onComplete(context)
218218
try {
219219
attrs.forEach { it.handle() }
220220
body.handle()
221221
} catch (e: CancellationException) {
222222
if (e !is Return)
223-
thisContextScript().logger.log(
223+
this.thisContextScript().logger.log(
224224
Level.WARNING, "You should not cancel command. If you need exit, using CommandInfo.Return()", e
225225
)
226226
} catch (e: Exception) {
227-
reply("[red]执行命令出现异常: {msg}".with("msg" to (e.message ?: "")))
227+
context.reply("[red]执行命令出现异常: {msg}".with("msg" to (e.message ?: "")))
228228
e.printStackTrace()
229229
}
230230
}
@@ -259,19 +259,19 @@ class CommandInfo(
259259
annotation class CommandBuilder
260260
}
261261

262-
@Suppress("DEPRECATION", "SUPERTYPE_IS_SUSPEND_EXTENSION_FUNCTION_TYPE")
263-
open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
262+
@Suppress("DEPRECATION")
263+
open class Commands : CommandHandler, TabCompleter {
264264
fun interface Hidden : CommandHandler {
265-
/** 当前命令是否可用, 用于[Commands.helpCommand]处理 */
266-
context(CommandContext) suspend fun visible(): Boolean
267-
context(CommandContext) override suspend fun handle() {
265+
/** 当前命令是否可用, 用于[Commands]处理 */
266+
suspend fun CommandContext.visible(): Boolean
267+
override suspend fun CommandContext.handle() {
268268
if (!visible()) returnReply("[red]该命令当前不可用".with())
269269
}
270270
}
271271

272272
data class Permission(val permission: String) : Hidden {
273-
context(CommandContext) override suspend fun visible(): Boolean = hasPermission(permission)
274-
context(CommandContext) override suspend fun handle() {
273+
override suspend fun CommandContext.visible(): Boolean = hasPermission(permission)
274+
override suspend fun CommandContext.handle() {
275275
if (!visible()) returnReply("[red]你没有执行该命令的权限".with())
276276
}
277277
}
@@ -281,11 +281,11 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
281281
open fun subCommands(): Map<String, CommandInfo> = nameMap
282282
fun getSub(name: String): CommandInfo? = subCommands()[name.lowercase()]
283283

284-
context(CommandContext) override fun canHandle(): Boolean = true
284+
override fun CommandContext.canHandle(): Boolean = true
285285
override suspend fun onComplete(context: CommandContext) = context.run { handle() }
286286

287-
context(CommandContext) override suspend fun handle() {
288-
context.onComplete(0) { subCommands().keys.toList() }
287+
override suspend fun CommandContext.handle() {
288+
onComplete(0) { subCommands().keys.toList() }
289289
if (arg.isEmpty()) return helpCommand.handle()
290290

291291
val name = arg.first()
@@ -395,13 +395,6 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
395395
addSub(this)
396396
}
397397

398-
//compatibility for [CommandInfo.body]
399-
@Deprecated(
400-
"use CommandHandler instead", level = DeprecationLevel.ERROR,
401-
replaceWith = ReplaceWith("this.handle()")
402-
)
403-
override suspend fun invoke(p1: CommandContext) = error("use CommandHandler")
404-
405398
object Root : Commands() {
406399
init {
407400
this += CommandInfo(thisContextScript(), "ScriptAgent", "ScriptAgent 控制指令".with(), listOf("sa")).apply {
@@ -449,6 +442,13 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
449442
}
450443
}
451444

445+
context(context: CommandContext)
446+
fun CommandHandler.canHandle() = context.canHandle()
447+
context(context: CommandContext)
448+
suspend inline fun CommandHandler.handle() = context.handle()
449+
context(context: CommandContext)
450+
suspend inline fun Commands.Hidden.visible() = context.visible()
451+
452452
@ScriptDsl
453453
inline fun Script.command(
454454
name: String,

0 commit comments

Comments
 (0)