Skip to content

Commit 7156257

Browse files
committed
✨(*) 适配K2和新版本SA
1 parent 440760d commit 7156257

39 files changed

Lines changed: 525 additions & 418 deletions

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ dependencies.constraints {
7979
api("dev.folia:folia-api:$bukkitVersion")
8080
}
8181
dependencies {
82-
val libraryVersion = "2.1.1"
82+
val libraryVersion = "2.1.4"
8383
api("cf.wayzer:ScriptAgent:${libraryVersion}")
8484
implementation("cf.wayzer:LibraryManager:1.6")
8585

loader/mindustry/src/Main.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Main(private val loader: Plugin) : Plugin(), CommonMain {
5959
Log.info("&bQQ交流群: 1033116078")
6060
val all = ScriptRegistry.allScripts { true }
6161
Log.info(
62-
"&b共找到${all.size}脚本,加载成功${all.count { it.scriptState.loaded }},启用成功${all.count { it.scriptState.enabled }},出错${all.count { it.failReason != null }}"
62+
"&b共找到${all.size}脚本,加载成功${all.count { it.scriptState.loaded }},启用成功${all.count { it.scriptState.enabled }},出错${all.count { !it.transaction.ready() }}"
6363
)
6464
if (!foundMain)
6565
Log.warn("&c未找到启动脚本(${Config.mainScript}),请下载安装脚本包,以发挥本插件功能")

scripts/bootStrap/generate.kts

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package bootStrap
22

3-
import cf.wayzer.scriptAgent.events.ScriptStateChangeEvent
3+
import cf.wayzer.scriptAgent.state.ConditionState
44
import cf.wayzer.scriptAgent.util.CASScriptPacker
55
import cf.wayzer.scriptAgent.util.DependencyManager
66
import cf.wayzer.scriptAgent.util.maven.Dependency
@@ -12,66 +12,37 @@ fun prepareBuiltin(outputFile: File = File("build/tmp/builtin.packed.zip")) {
1212
val scripts = ScriptRegistry.allScripts { it.scriptState.loaded }
1313
.mapNotNull { it.compiledScript }
1414
println("prepare Builtin for ${scripts.size} scripts.")
15-
@OptIn(SAExperimentalApi::class)
1615
CASScriptPacker(outputFile.outputStream())
1716
.use { scripts.forEach(it::add) }
1817
}
1918

20-
suspend fun compileOnlyLoad(script: ScriptInfo) {
21-
Config.logger.info("编译脚本 ${script.id}")
22-
val compiled = try {
23-
@OptIn(SAExperimentalApi::class)
24-
ScriptManager.compileScript(script.source)
25-
} catch (e: Exception) {
26-
val msg = if (e is IllegalStateException) e.message else e.toString()
27-
script.failReason = "编译失败: $msg"
28-
return
29-
}
30-
try {
31-
compiled.loadLibraries()
32-
} catch (e: Exception) {
33-
script.failReason = "依赖下载失败:$e"
34-
return
35-
}
36-
script.compiledScript = compiled
37-
script.stateUpdateForce(ScriptState.Loaded).join()
38-
}
39-
40-
//Replaced with compileOnlyLoad
41-
listenTo<ScriptStateChangeEvent.Cancellable> {
42-
if (next == ScriptState.Loading) {
43-
cancelled = true
44-
compileOnlyLoad(script)
45-
}
46-
}
47-
4819
onEnable {
4920
if (id != Config.mainScript)
5021
return@onEnable ScriptManager.disableScript(this, "仅可通过SAMAIN启用")
51-
//so we can use `listenTo` in main
52-
launch { main() }
22+
ScriptManager.afterTransaction { main() }
5323
}
5424
suspend fun main() {
5525
DependencyManager {
5626
addRepository("https://www.jitpack.io/")
5727
require(Dependency.parse("com.github.TinyLake.MindustryX:core:v2025.10.X21"))
5828
loadToClassLoader(Config.mainClassloader)
5929
}
60-
ScriptManager.transaction {
30+
ScriptManager.transactionV2 {
31+
//Load Kcp
32+
enable("kcp")
33+
execute().printResult()
34+
6135
if (Config.args.isEmpty())
62-
addAll()
36+
compileOnly("")
6337
else
64-
Config.args.forEach { add(it) }
65-
66-
load()
67-
}
68-
val fail = ScriptRegistry.allScripts { it.failReason != null }
69-
println("共加载${ScriptRegistry.allScripts { it.scriptState != ScriptState.Found }.size}个脚本,失败${fail.size}")
70-
fail.forEach {
71-
println("\t${it.id}: ${it.failReason}")
72-
}
73-
if (System.getProperty("ScriptAgent.PreparePack") != null) {
74-
println("Finish pack in ${measureTimeMillis { prepareBuiltin() }}ms")
38+
Config.args.forEach { compileOnly(it) }
39+
}.run {
40+
val fail = conditions.values.filter { it.status == ConditionState.Status.Success }
41+
println("共加载${conditions.size}个脚本,失败${fail.size}")
42+
printResult()
43+
if (System.getProperty("ScriptAgent.PreparePack") != null) {
44+
println("Finish pack in ${measureTimeMillis { prepareBuiltin() }}ms")
45+
}
46+
exitProcess(fail.size)
7547
}
76-
exitProcess(fail.size)
7748
}

scripts/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ dependencies {
5151

5252
defineModule("wayzer") {
5353
dependsOnModule("coreMindustry")
54+
dependsOnModule("coreLibrary/extApi")
5455
api("com.google.guava:guava:30.1-jre")
5556
//wayzer/ext/profiler
5657
implementation("tools.profiler:async-profiler:4.1")

scripts/coreLibrary/commands/control.kts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,29 @@ command("scan", "重新扫描脚本".with(), commands = Commands.controlCommand)
1818
reply("[green]扫描完成,新发现{count}脚本".with("count" to (now - old)))
1919
}
2020
}
21+
command("listFailed", "列出所有故障脚本".with(), commands = Commands.controlCommand) {
22+
usage = "[prefix]"
23+
requirePermission("scriptAgent.control.list")
24+
aliases = listOf("fail", "failed")
25+
body {
26+
val prefix = arg.firstOrNull().orEmpty()
27+
val scripts = ScriptRegistry.allScripts {
28+
it.id.startsWith(prefix) && !it.transaction.ready()
29+
}
30+
for (info in scripts) {
31+
reply(buildString {
32+
appendLine("[${info.scriptState}] ${info.id}")
33+
info.conditions.filter { it.status != ConditionState.Status.Success }.forEach { c ->
34+
c.display().forEach {
35+
append(" ")
36+
appendLine(it)
37+
}
38+
}
39+
deleteAt(length - 1)
40+
}.asPlaceHoldString())
41+
}
42+
}
43+
}
2144
command("list", "列出所有模块或模块内所有脚本".with(), commands = Commands.controlCommand) {
2245
usage = "[module/fail]"
2346
requirePermission("scriptAgent.control.list")
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package coreLib.extApi
22

3+
import org.h2.mvstore.MVMap
34
import org.h2.mvstore.type.DataType
45
import org.h2.mvstore.type.StringDataType
56

67
//@file:Depends("coreLibrary/extApi/KVStore")
78
interface KVStore {
89
fun <V> open(name: String, type: DataType<V>) = open(name, type, StringDataType.INSTANCE)
9-
fun <K, V> open(name: String, key: DataType<K>, type: DataType<V>)
10+
fun <K, V> open(name: String, key: DataType<K>, type: DataType<V>): MVMap<K, V>
1011
}

scripts/coreLibrary/extApi/lib/RpcService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java.rmi.Remote
44

55
//@file:Depends("coreLibrary/extApi/rpcService")
66
interface RpcService {
7+
val isMaster: Boolean
78
fun <T : Remote> get(inf: Class<T>): Remote
89
fun <T : Remote> register(inf: Class<T>, factory: () -> T)
910
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
package coreMindustry
1+
package coreMindustry.util
22

3-
import mindustry.game.EventType.TextInputEvent
3+
import cf.wayzer.scriptAgent.Event
4+
import cf.wayzer.scriptAgent.define.Script
5+
import coreLibrary.lib.util.nextEvent
6+
import kotlinx.coroutines.withTimeoutOrNull
7+
import mindustry.gen.Call
8+
import mindustry.gen.Player
49
import kotlin.random.Random
510

611
data class OnTextInputResult(val player: Player, val id: Int, val text: String?) : Event {
712
companion object : Event.Handler()
813
}
914

10-
listen<TextInputEvent> {
11-
OnTextInputResult(it.player, it.textInputId, it.text).launchEmit(coroutineContext + Dispatchers.game)
12-
}
13-
15+
@Suppress("unused")
16+
context(script: Script)
1417
suspend fun textInput(
1518
player: Player,
1619
title: String,
@@ -22,6 +25,5 @@ suspend fun textInput(
2225
): String? = withTimeoutOrNull(timeoutMillis.toLong()) {
2326
val id = Random.nextInt(Int.MIN_VALUE, 0)
2427
Call.textInput(player.con, id, title, message, lengthLimit, default, isNumeric)
25-
nextEvent<OnTextInputResult> { it.player == player && it.id == id }.text
26-
}
27-
export(::textInput)
28+
script.nextEvent<OnTextInputResult> { it.player == player && it.id == id }.text
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package coreMindustry.util
2+
3+
import mindustry.game.EventType.TextInputEvent
4+
5+
listen<TextInputEvent> {
6+
OnTextInputResult(it.player, it.textInputId, it.text).launchEmit(coroutineContext + Dispatchers.game)
7+
}
8+

scripts/metadata/coreLibrary.metadata

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ FQ_NAME Postgres
5353
+DEPENDS coreLibrary/db module
5454
+IMPORT MavenDepends org.postgresql:postgresql:42.7.5
5555

56+
ID coreLibrary/extApi/KVStore
57+
FQ_NAME KVStore
58+
+DEPENDS coreLibrary module
59+
+IMPORT MavenDependsSingle com.h2database:h2-mvstore:2.3.232
60+
+IMPORT SourceFile ./lib/KVStore.kt
61+
62+
ID coreLibrary/extApi/rpcService
63+
FQ_NAME RpcService
64+
+DEPENDS coreLibrary module
65+
+IMPORT SourceFile ./lib/RpcService.kt
66+
5667
ID coreLibrary/lang
5768
FQ_NAME coreLibrary.Lang
5869
+DEPENDS coreLibrary module

0 commit comments

Comments
 (0)