diff --git a/cpp/demo.cpp b/cpp/demo.cpp index 35b69d620..f8a8fa27b 100644 --- a/cpp/demo.cpp +++ b/cpp/demo.cpp @@ -78,9 +78,13 @@ class Main:public CPPPlugin { // 监听群信息 procession->registerEvent([=](GroupMessageEvent e) { e.group.sendMsg("HI*1", -1); + Sleep(5000); e.group.sendMsg("HI*2", -1); + Sleep(5000); e.group.sendMsg("HI*3", -1); + Sleep(5000); e.group.sendMsg("HI*4", -1); + Sleep(5000); // SYSTEMTIME st = { 0 }; // GetLocalTime(&st); //获取当前时间 可精确到ms // logger->info(to_string(st.wHour)+":"+to_string(st.wMinute)+":"+to_string(st.wSecond)+":"+to_string(st.wMilliseconds)); diff --git a/kotlin/gradle/libs.versions.toml b/kotlin/gradle/libs.versions.toml index ae2e442df..a4b312bc7 100644 --- a/kotlin/gradle/libs.versions.toml +++ b/kotlin/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] kotlinVersion = "1.5.10" miraiVersion = "2.7.0" -miraiCPVersion = "2.7-RC" +miraiCPVersion = "2.7.0" [libraries] mirai-core-api = { module = "net.mamoe:mirai-core-api", version.ref = "miraiVersion" } diff --git a/kotlin/loader/src/main/kotlin/KotlinMain.kt b/kotlin/loader/src/main/kotlin/KotlinMain.kt index 29856e125..02e91ddeb 100644 --- a/kotlin/loader/src/main/kotlin/KotlinMain.kt +++ b/kotlin/loader/src/main/kotlin/KotlinMain.kt @@ -50,6 +50,7 @@ object KotlinMain { logger.error("Error: 无可登录账号,请检查config.json内容") return } + PublicShared.threadNum = c.config?.threadNum ?: let { PublicShared.threadNum } c.accounts?.filter { it.autoLogin == true }?.forEach { it.login() logined = true diff --git a/kotlin/loader/src/main/kotlin/console/Console.kt b/kotlin/loader/src/main/kotlin/console/Console.kt index 1de004f02..41065ca5f 100644 --- a/kotlin/loader/src/main/kotlin/console/Console.kt +++ b/kotlin/loader/src/main/kotlin/console/Console.kt @@ -65,15 +65,6 @@ object Console { err.printStackTrace() return@let terminal.also { it.resume() } }.getOrThrow() - // terminal.setOnClose { -// response = NoConsole -// } -// terminal.resume() -// val pumpThread = pumpField[terminal] as? Thread ?: return@let NoConsole -// @Suppress("ControlFlowWithEmptyBody") -// while (pumpThread.state == Thread.State.NEW); -// Thread.sleep(1000) -// terminal.setOnClose(null) return@let terminal.also { it.resume() } } terminal.also { it.resume() } diff --git a/kotlin/loader/src/test/kotlin/group/tempTest.kt b/kotlin/loader/src/test/kotlin/group/tempTest.kt new file mode 100644 index 000000000..b6efe262e --- /dev/null +++ b/kotlin/loader/src/test/kotlin/group/tempTest.kt @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2020-2021 Eritque arcus and contributors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version(in your opinion). + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package tech.eritquearcus.miraicp.loader.group + +import group.groupMessage +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +import net.mamoe.mirai.utils.MiraiInternalApi +import net.mamoe.mirai.utils.MiraiLogger +import net.mamoe.mirai.utils.PlatformLogger +import org.junit.jupiter.api.Test +import tech.eritquearcus.miraicp.loader.main + +class tempTest { + @Test + @OptIn( + MiraiInternalApi::class, net.mamoe.mirai.utils.MiraiExperimentalApi::class, + kotlin.time.ExperimentalTime::class + ) + fun tempGroupMessageTest() { + runBlocking { + main(arrayOf("../config.json")) + MiraiLogger.setDefaultLoggerCreator { + PlatformLogger(it!!) { s -> + println(s) + } + } + // 3 events/s in 10s + launch { + for (i in 1..2) { + launch { + groupMessage() + } + delay(200) + } + } + launch { + for (i in 1..2) { + launch { + groupMessage() + } + } + } + launch { + for (i in 1..2) { + launch { + groupMessage() + } + } + } + launch { + for (i in 1..2) { + launch { + groupMessage() + } + } + } + } + } +} \ No newline at end of file diff --git a/kotlin/plugin/src/main/kotlin/PluginMain.kt b/kotlin/plugin/src/main/kotlin/PluginMain.kt index 064ccbb1b..185b0c9f2 100644 --- a/kotlin/plugin/src/main/kotlin/PluginMain.kt +++ b/kotlin/plugin/src/main/kotlin/PluginMain.kt @@ -63,7 +63,9 @@ object PluginMain : KotlinPlugin( ) return } - }.readText(), Config.pluginConfig::class.java).pluginConfig.forEach { i -> + }.readText(), Config.pluginConfig::class.java).apply { + PublicShared.threadNum = this.config?.threadNum ?: let { PublicShared.threadNum } + }.pluginConfig.forEach { i -> val it = i.path val d = i.dependencies?.filter { p -> File(p).let { f -> diff --git a/kotlin/shared/src/main/kotlin/CPP_lib.kt b/kotlin/shared/src/main/kotlin/CPP_lib.kt index 3219fbe20..a82c525fd 100644 --- a/kotlin/shared/src/main/kotlin/CPP_lib.kt +++ b/kotlin/shared/src/main/kotlin/CPP_lib.kt @@ -87,7 +87,6 @@ class CPP_lib ( //cd shared/build/classes/kotlin/main && javap.exe -s tech.eritquearcus.miraicp.shared.CPP_lib companion object{ var test: Boolean = false - //send MiraiCode private fun KSend(source: String, miraiCode: Boolean, retryTime: Int): String = runBlocking { val tmp = gson.fromJson(source, Config.SendRequest::class.java) diff --git a/kotlin/shared/src/main/kotlin/Config.kt b/kotlin/shared/src/main/kotlin/Config.kt index 9ec865966..e9be678bb 100644 --- a/kotlin/shared/src/main/kotlin/Config.kt +++ b/kotlin/shared/src/main/kotlin/Config.kt @@ -134,11 +134,17 @@ class Config { val dependencies: List? ) + data class c( + val threadNum: Int? + ) + data class pluginConfig( - val pluginConfig: List + val pluginConfig: List, + val config: c? ) data class accounts( + val config: c?, val accounts: List?, val cppPaths: List ) { diff --git a/kotlin/shared/src/main/kotlin/PublicShared.kt b/kotlin/shared/src/main/kotlin/PublicShared.kt index 62b4e2090..10b6f8431 100644 --- a/kotlin/shared/src/main/kotlin/PublicShared.kt +++ b/kotlin/shared/src/main/kotlin/PublicShared.kt @@ -50,7 +50,6 @@ import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage import net.mamoe.mirai.utils.RemoteFile.Companion.uploadFile import org.json.JSONObject import java.io.File -import java.text.SimpleDateFormat import java.util.* import kotlin.concurrent.schedule @@ -67,7 +66,7 @@ object PublicShared { val cpp: ArrayList = arrayListOf() val gson: Gson = Gson() lateinit var logger: MiraiLogger - const val now_tag = "v2.7-RC" + const val now_tag = "v2.7.0" val logger4plugins: MutableMap = mutableMapOf() val disablePlugins = arrayListOf() val loadedPlugins = arrayListOf() @@ -841,13 +840,15 @@ object PublicShared { //定时任务 fun scheduling(time: Long, msg: String):String { Timer("Timer", false).schedule(time) { - cpp.Event( - Gson().toJson( - Config.TimeOutEvent( - msg + runBlocking { + cpp.Event( + Gson().toJson( + Config.TimeOutEvent( + msg + ) ) ) - ) + } } return "Y" } @@ -884,14 +885,16 @@ object PublicShared { } "Y" } - else->"EA" + else -> "EA" } } - fun onDisable() = cpp.forEach {it.PluginDisable()} + fun onDisable() = cpp.forEach { it.PluginDisable() } + + var threadNum = 5 @OptIn(MiraiExperimentalApi::class) - fun onEnable(eventChannel: EventChannel){ + fun onEnable(eventChannel: EventChannel) { //配置文件目录 "${dataFolder.absolutePath}/" eventChannel.subscribeAlways { //好友信息 @@ -910,8 +913,6 @@ object PublicShared { } eventChannel.subscribeAlways { //群消息 - val sdf = SimpleDateFormat("HH:mm:ss.SSS") - val s = sdf.format(Date()).toString() cpp.Event( gson.toJson( Config.GroupMessage( @@ -922,7 +923,6 @@ object PublicShared { ) ) ) - logger.info(s + " | " + sdf.format(Date()).toString()) } eventChannel.subscribeAlways { friend_cache.add(this.member) diff --git a/kotlin/shared/src/main/kotlin/Ulits.kt b/kotlin/shared/src/main/kotlin/Ulits.kt index bdc2e4022..c7f8c6374 100644 --- a/kotlin/shared/src/main/kotlin/Ulits.kt +++ b/kotlin/shared/src/main/kotlin/Ulits.kt @@ -17,27 +17,42 @@ package tech.eritquearcus.miraicp.shared -import net.mamoe.mirai.contact.* - -fun ArrayList.Event(content: String){ - when{ - PublicShared.disablePlugins.isNotEmpty()->{ - this.filter { - !PublicShared.disablePlugins.contains(it.config.name) - }.forEach { - it.Event(content) +import kotlinx.coroutines.asCoroutineDispatcher +import kotlinx.coroutines.runInterruptible +import net.mamoe.mirai.contact.Friend +import net.mamoe.mirai.contact.Group +import net.mamoe.mirai.contact.Member +import net.mamoe.mirai.contact.nameCardOrNick +import java.util.concurrent.Executors + +private val cc by lazy { Executors.newFixedThreadPool(PublicShared.threadNum).asCoroutineDispatcher() } + +private suspend inline fun T.runInTP( + crossinline block: T.() -> R, +): R = runInterruptible(context = cc, block = { block() }) + +suspend fun ArrayList.Event(content: String) { + runInTP { + when { + PublicShared.disablePlugins.isNotEmpty() -> { + this.filter { + !PublicShared.disablePlugins.contains(it.config.name) + }.forEach { + it.Event(content) + } + } + else -> { + this.forEach { it.Event(content) } } - } - else->{ - this.forEach { it.Event(content) } } } } -internal fun Group.toContact():Config.Contact= + +internal fun Group.toContact(): Config.Contact = Config.Contact(2, this.id, 0, this.name, this.bot.id) -internal fun Member.toContact():Config.Contact = +internal fun Member.toContact(): Config.Contact = Config.Contact(3, this.id, this.group.id, this.nameCardOrNick, this.bot.id) internal fun Friend.toContact():Config.Contact =