Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
完整解决 #76 的问题, 配置文件进阶格式更改
Browse files Browse the repository at this point in the history
Former-commit-id: 67b5491
  • Loading branch information
Nambers committed Aug 28, 2021
1 parent 9a38965 commit d5a8344
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 40 deletions.
4 changes: 4 additions & 0 deletions cpp/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ class Main:public CPPPlugin {
// 监听群信息
procession->registerEvent<GroupMessageEvent>([=](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));
Expand Down
2 changes: 1 addition & 1 deletion kotlin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
1 change: 1 addition & 0 deletions kotlin/loader/src/main/kotlin/KotlinMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions kotlin/loader/src/main/kotlin/console/Console.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
76 changes: 76 additions & 0 deletions kotlin/loader/src/test/kotlin/group/tempTest.kt
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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()
}
}
}
}
}
}
4 changes: 3 additions & 1 deletion kotlin/plugin/src/main/kotlin/PluginMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
1 change: 0 additions & 1 deletion kotlin/shared/src/main/kotlin/CPP_lib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion kotlin/shared/src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,17 @@ class Config {
val dependencies: List<String>?
)

data class c(
val threadNum: Int?
)

data class pluginConfig(
val pluginConfig: List<cppPath>
val pluginConfig: List<cppPath>,
val config: c?
)

data class accounts(
val config: c?,
val accounts: List<Account>?,
val cppPaths: List<cppPath>
) {
Expand Down
26 changes: 13 additions & 13 deletions kotlin/shared/src/main/kotlin/PublicShared.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -67,7 +66,7 @@ object PublicShared {
val cpp: ArrayList<CPP_lib> = 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<String, MiraiLogger> = mutableMapOf()
val disablePlugins = arrayListOf<String>()
val loadedPlugins = arrayListOf<String>()
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -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<Event>){
fun onEnable(eventChannel: EventChannel<Event>) {
//配置文件目录 "${dataFolder.absolutePath}/"
eventChannel.subscribeAlways<FriendMessageEvent> {
//好友信息
Expand All @@ -910,8 +913,6 @@ object PublicShared {
}
eventChannel.subscribeAlways<GroupMessageEvent> {
//群消息
val sdf = SimpleDateFormat("HH:mm:ss.SSS")
val s = sdf.format(Date()).toString()
cpp.Event(
gson.toJson(
Config.GroupMessage(
Expand All @@ -922,7 +923,6 @@ object PublicShared {
)
)
)
logger.info(s + " | " + sdf.format(Date()).toString())
}
eventChannel.subscribeAlways<MemberLeaveEvent.Kick> {
friend_cache.add(this.member)
Expand Down
43 changes: 29 additions & 14 deletions kotlin/shared/src/main/kotlin/Ulits.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,42 @@

package tech.eritquearcus.miraicp.shared

import net.mamoe.mirai.contact.*

fun ArrayList<CPP_lib>.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, R> T.runInTP(
crossinline block: T.() -> R,
): R = runInterruptible(context = cc, block = { block() })

suspend fun ArrayList<CPP_lib>.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 =
Expand Down

0 comments on commit d5a8344

Please sign in to comment.