From 592d28044e44e82b826db3b2214ce9727d50dcfe Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Fri, 6 Mar 2020 20:52:58 +0800 Subject: [PATCH 1/9] Console version checker --- .../net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt index b38852936..599b013c2 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/ConsoleUpdator.kt @@ -68,10 +68,10 @@ object ConsoleUpdator{ ).asSequence() .map { it.value.drop(1).dropLast(1) } .maxBy { - it.split('.').foldRightIndexed(0) { index: Int, s: String, acc: Int -> - acc + 100.0.pow(2 - index).toInt() * (s.toIntOrNull() ?: 0) - } - }!! + it.split('.').foldRightIndexed(0) { index: Int, s: String, acc: Int -> + acc + 100.0.pow(2 - index).toInt() * (s.toIntOrNull() ?: 0) + } + }!! } catch (e: Exception) { println("Failed to fetch newest Console version, please seek for help") e.printStackTrace() From 140cd538b55ea09e01338c7611438045b5956d18 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Fri, 6 Mar 2020 21:50:03 +0800 Subject: [PATCH 2/9] downloader --- .../net/mamoe/mirai/console/wrapper/Downloader.kt | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt index c28cfe738..02f54c3ea 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt @@ -104,20 +104,13 @@ suspend fun HttpClient.downloadMavenPomAsString( version: String ):String{ return kotlin.runCatching { - Http.get( + this.get( aliyunPath.buildPath(groupName,projectName,version,"pom") ) }.getOrElse { - try { - Http.get( - aliyunPath.buildPath(groupName, projectName, version, "pom") - ) - }catch (e:Exception){ - if(e.message?.contains("404 Not Found") == true) { - return "" - } - throw e - } + this.get( + aliyunPath.buildPath(groupName, projectName, version, "pom") + ) } } From 1b510d3d7228998c697c1084b56c3bdc5755f8a5 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Fri, 6 Mar 2020 21:52:57 +0800 Subject: [PATCH 3/9] safe download --- .../net/mamoe/mirai/console/wrapper/Downloader.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt index 02f54c3ea..6bb81d5a2 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt @@ -39,7 +39,15 @@ inline fun tryNTimesOrQuit(repeat: Int, errorHint: String, block: (Int) -> R suspend inline fun HttpClient.downloadRequest(url: String): ByteReadChannel { - return this.get(url).content + return with(this.get(url)){ + if(this.status.value == 404 || this.status.value == 403){ + error("File not found") + } + if(this.headers["status"] !=null && this.headers["status"] == "404"){ + error("File not found") + } + this.content + } } private val jcenterPath = "https://jcenter.bintray.com/{group}/{project}/{version}/:{project}-{version}.{extension}" From 1a24935c818c6cd70a9a3c30dd3fe40e85d79f00 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Fri, 6 Mar 2020 21:54:08 +0800 Subject: [PATCH 4/9] safe download --- .../main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt index 6bb81d5a2..f10b1e150 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/Downloader.kt @@ -85,7 +85,7 @@ suspend fun HttpClient.downloadMaven( ) }.getOrElse { downloadRequest( - aliyunPath.buildPath(groupName,projectName,version,extension) + jcenterPath.buildPath(groupName,projectName,version,extension) ) } } From 8ce40ea41efd2950fe99012260e5da6727db9509 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Fri, 6 Mar 2020 21:57:05 +0800 Subject: [PATCH 5/9] GC --- .../kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt index ee649b342..1269ff91f 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt @@ -33,6 +33,12 @@ val contentPath by lazy { object WrapperMain { @JvmStatic fun main(args: Array) { + GlobalScope.launch{ + while (true) { + delay(1000*60*5) + System.gc() + } + } println("You are running Mirai-Console-Wrapper under " + System.getProperty("user.dir")) var type = WrapperProperties.determineConsoleType(WrapperProperties.content) From f29c205faa2e83960a9cbb07b8d2ecbbc166da20 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Fri, 6 Mar 2020 21:59:25 +0800 Subject: [PATCH 6/9] console wrapper 0.11 --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 63ba1fe10..68ea555d2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ kotlin.code.style=official # config miraiVersion=0.24.1 miraiConsoleVersion=0.3.2 -miraiConsoleWrapperVersion=0.0.1 +miraiConsoleWrapperVersion=0.1.1 kotlin.incremental.multiplatform=true kotlin.parallel.tasks.in.project=true # kotlin From 66b9e59634219fde6c50b5ad19121576a7967b4c Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Fri, 6 Mar 2020 22:27:21 +0800 Subject: [PATCH 7/9] clean up --- .../kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt | 7 ------- 1 file changed, 7 deletions(-) diff --git a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt index 1269ff91f..caee74998 100644 --- a/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt +++ b/mirai-console-wrapper/src/main/kotlin/net/mamoe/mirai/console/wrapper/WrapperMain.kt @@ -9,17 +9,10 @@ @file:Suppress("EXPERIMENTAL_API_USAGE") package net.mamoe.mirai.console.wrapper -import io.ktor.client.HttpClient -import io.ktor.client.engine.cio.CIO -import io.ktor.client.request.get -import io.ktor.client.statement.HttpResponse -import io.ktor.utils.io.ByteReadChannel -import io.ktor.utils.io.jvm.javaio.copyTo import kotlinx.coroutines.* import java.io.File import java.net.URLClassLoader import java.util.* -import kotlin.system.exitProcess val contentPath by lazy { From d411df85ca5396162e85a930b73850d2421cbb10 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Sat, 7 Mar 2020 18:12:24 +0800 Subject: [PATCH 8/9] plugin API fixed --- .../mamoe/mirai/console/plugins/PluginBase.kt | 6 +++- .../mirai/console/plugins/PluginManager.kt | 34 ++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt index c8bec6dc1..dc8c38066 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt @@ -83,7 +83,11 @@ abstract class PluginBase @JvmOverloads internal fun disable(throwable: CancellationException? = null) { this.coroutineContext[Job]!!.cancelChildren(throwable) - this.onDisable() + try { + this.onDisable() + }catch (e:Exception){ + logger.info(e) + } } internal var pluginName: String = "" diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginManager.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginManager.kt index d8208139f..0359249b8 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginManager.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginManager.kt @@ -42,7 +42,11 @@ object PluginManager { fun onCommand(command: Command, sender: CommandSender, args: List) { nameToPluginBaseMap.values.forEach { - it.onCommand(command, sender, args) + try { + it.onCommand(command, sender, args) + }catch (e:Exception){ + logger.info(e) + } } } @@ -194,7 +198,27 @@ object PluginManager { } nameToPluginBaseMap.values.forEach { - it.enable() + try { + it.onLoad() + }catch (ignored:Exception){ + if(ignored is CancellationException) { + logger.info(ignored) + logger.info(it.pluginName + "Failed to load, disabling it") + it.disable(ignored) + } + } + } + + nameToPluginBaseMap.values.forEach { + try { + it.enable() + }catch (ignored:Exception){ + logger.info(ignored) + logger.info(it.pluginName + "Failed to enable, disabling it") + if(ignored is CancellationException) { + it.disable(ignored) + } + } } logger.info("""加载了${nameToPluginBaseMap.size}个插件""") @@ -213,9 +237,9 @@ object PluginManager { * 根据插件名字找Jar的文件 * null => 没找到 */ - fun getJarPath(pluginName: String): File? { + fun getJarFileByName(pluginName: String): File? { File(pluginsPath).listFiles()?.forEach { file -> - if (file != null && file.extension == "jar") { + if (file != null && file.extension == "jar") { val jar = JarFile(file) val pluginYml = jar.entries().asSequence().filter { it.name.toLowerCase().contains("plugin.yml") }.firstOrNull() @@ -240,7 +264,7 @@ object PluginManager { * null => 没找到 */ fun getFileInJarByName(pluginName: String, toFind: String): InputStream? { - val jarFile = getJarPath(pluginName) ?: return null + val jarFile = getJarFileByName(pluginName) ?: return null val jar = JarFile(jarFile) val toFindFile = jar.entries().asSequence().filter { it.name == toFind }.firstOrNull() ?: return null From 5d0e600f9f3f3aa8e36bbda212e63ff8da3eca5d Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Sat, 7 Mar 2020 21:55:56 +0800 Subject: [PATCH 9/9] Events and Tasks --- .../mamoe/mirai/console/events/Events.java | 66 ++++++++++++++++++ .../mamoe/mirai/console/events/EventsImpl.kt | 16 +++++ .../mirai/console/scheduler/SchedulerTask.kt | 69 +++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/Events.java create mode 100644 mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/EventsImpl.kt create mode 100644 mirai-console/src/main/kotlin/net/mamoe/mirai/console/scheduler/SchedulerTask.kt diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/Events.java b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/Events.java new file mode 100644 index 000000000..287d1c3a9 --- /dev/null +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/Events.java @@ -0,0 +1,66 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.events; + +import kotlinx.coroutines.GlobalScope; +import net.mamoe.mirai.event.Event; +import net.mamoe.mirai.event.Listener; +import net.mamoe.mirai.event.ListeningStatus; +import net.mamoe.mirai.event.internal.EventInternalJvmKt; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * 事件处理 + */ +public final class Events { + + /** + * 监听一个事件, 当 {@code onEvent} 返回 {@link ListeningStatus#STOPPED} 时停止监听. + * 机器人离线后不会停止监听. + * + * @param eventClass 事件类 + * @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听. + * @param 事件类型 + * @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止. + */ + @NotNull + public static Listener subscribe(@NotNull Class eventClass, @NotNull Function onEvent) { + return EventInternalJvmKt._subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent); + } + + /** + * 监听一个事件, 直到手动停止. + * 机器人离线后不会停止监听. + * + * @param eventClass 事件类 + * @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听. + * @param 事件类型 + * @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止. + */ + @NotNull + public static Listener subscribeAlways(@NotNull Class eventClass, @NotNull Consumer onEvent) { + return EventInternalJvmKt._subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent); + } + + /** + * 阻塞地广播一个事件. + * + * @param event 事件 + * @param 事件类型 + * @return {@code event} 本身 + */ + @NotNull + public static E broadcast(@NotNull E event) { + return EventsImplKt.broadcast(event); + } +} \ No newline at end of file diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/EventsImpl.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/EventsImpl.kt new file mode 100644 index 000000000..baf44f72e --- /dev/null +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/EventsImpl.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.events; + +import kotlinx.coroutines.runBlocking +import net.mamoe.mirai.event.Event +import net.mamoe.mirai.event.broadcast + +internal fun broadcast(e: E): E = runBlocking { e.broadcast() } \ No newline at end of file diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/scheduler/SchedulerTask.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/scheduler/SchedulerTask.kt new file mode 100644 index 000000000..ac145af57 --- /dev/null +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/scheduler/SchedulerTask.kt @@ -0,0 +1,69 @@ +package net.mamoe.mirai.console.scheduler + +import net.mamoe.mirai.console.plugins.PluginBase + +interface SchedulerTask{ + abstract fun onTick(i:Long) + abstract fun onRun() +} + +abstract class RepeatTask( + val intervalInMs: Int +):SchedulerTask{ + + override fun onTick(i: Long) { + if(i%intervalInMs == 0L){ + onRun() + } + } + + companion object{ + fun of( + intervalInMs: Int, runnable: () -> Unit + ):RepeatTask{ + return AnonymousRepeatTask( + intervalInMs, runnable + ) + } + } +} + +internal class AnonymousRepeatTask( + intervalInMs: Int, private val runnable: () -> Unit +): RepeatTask(intervalInMs){ + override fun onRun() { + runnable.invoke() + } +} + +fun T.repeatTask( + intervalInMs: Int, runnable: () -> Unit +):RepeatTask{ + return AnonymousRepeatTask( + intervalInMs, runnable + ) +} + + +fun repeatTask(){ + +} + +class X: PluginBase() { + override fun onLoad() { + //kotlin + this.repeatTask(5){ + + } + //java1 + RepeatTask.of(5){ + + } + //java2 + class Xtask:RepeatTask(5){ + override fun onRun() { + + } + } + } +} \ No newline at end of file