Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
支持lua语言的qq bot快速开发框架,使用lua轻松创造qq bot。

## 开发文档
[lua mirai doc](https://ooooonly.gitee.io/lua-mirai-doc)
[lua mirai doc](https://only52607.github.io/lua-mirai/)

## Android

Expand Down
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ subprojects {
apply plugin: 'org.jetbrains.kotlin.jvm'

dependencies {
def mirai_core_version = "2.9.2"
def mirai_core_version = "2.11.0"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "net.mamoe:mirai-core-api:$mirai_core_version"
Expand Down Expand Up @@ -74,5 +74,13 @@ publishing {
artifact luaktAdapterProject.tasks.sourcesJar
artifact luaktAdapterProject.tasks.javadocJar
}

configuration(MavenPublication) {
setArtifactId("configuration")
def luaktAdapterProject = project(':lua-mirai-configuration')
from luaktAdapterProject.components.java
artifact luaktAdapterProject.tasks.sourcesJar
artifact luaktAdapterProject.tasks.javadocJar
}
}
}
9 changes: 9 additions & 0 deletions lua-mirai-configuration/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.10'
}

dependencies {
implementation project(":lua-mirai-core")
runtimeOnly project(":lua-mirai-adapter-luakt")
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.only52607.luamirai.configuration

import com.github.only52607.luamirai.core.script.BotScriptSource
import kotlinx.serialization.Serializable
import java.io.InputStream
import java.nio.charset.Charset

/**
* ClassName: ConfigurableScriptSource
* Description:
* date: 2022/5/21 10:42
* @author ooooonly
* @version
*/
@Serializable(ConfigurableScriptSourceSerializer::class)
class ConfigurableScriptSource(
val source: BotScriptSource,
var alias: String? = null,
var autoStart: Boolean = false
) : BotScriptSource(
lang = source.lang,
name = source.name,
size = source.size,
charset = source.charset,
) {
override val inputStream: InputStream
get() = source.inputStream

override fun toString(): String {
return source.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.github.only52607.luamirai.configuration

import com.github.only52607.luamirai.core.script.BotScriptSource
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonPrimitive
import java.io.File
import java.net.URL

/**
* ClassName: ConfigurableScriptSourceSerializer
* Description:
* date: 2022/5/21 10:47
* @author ooooonly
* @version
*/
object ConfigurableScriptSourceSerializer : KSerializer<ConfigurableScriptSource> {
private const val TYPE_FILE = "file"
private const val TYPE_CONTENT = "content"
private const val TYPE_URL = "url"
private const val TYPE_UNKNOWN = "unknown"

override val descriptor: SerialDescriptor =
buildClassSerialDescriptor("ConfigurableScriptSource") {
element<String>("type")
element<String>("value")
element<String>("alias")
element<Boolean>("autoStart")
element<String>("lang")
}

override fun deserialize(decoder: Decoder): ConfigurableScriptSource {
decoder.decodeStructure(descriptor) {
var type = "unknown"
var value = ""
var alias = ""
var autoStart = false
var lang = "lua"
while (true) {
when (val index = decodeElementIndex(descriptor)) {
0 -> type = decodeStringElement(descriptor, 0)
1 -> value = decodeStringElement(descriptor, 1)
2 -> alias = decodeStringElement(descriptor, 2)
3 -> autoStart = decodeBooleanElement(descriptor, 3)
4 -> lang = decodeStringElement(descriptor, 4)
CompositeDecoder.DECODE_DONE -> break
else -> error("Unexpected index: $index")
}
}
val source = when (type) {
TYPE_FILE -> BotScriptSource.FileSource(File(value), lang)
TYPE_CONTENT -> BotScriptSource.StringSource(value, lang)
TYPE_URL -> BotScriptSource.URLSource(URL(value), lang)
else -> throw IllegalArgumentException("Unsupported script type $type")
}
return ConfigurableScriptSource(source, alias, autoStart)
}
}

override fun serialize(encoder: Encoder, value: ConfigurableScriptSource) {
return encoder.encodeStructure(descriptor) {
when (value.source) {
is BotScriptSource.FileSource -> {
encodeStringElement(descriptor, 0, TYPE_FILE)
encodeStringElement(descriptor, 1, value.source.file.path)
}
is BotScriptSource.StringSource -> {
encodeStringElement(descriptor, 0, TYPE_CONTENT)
encodeStringElement(descriptor, 1, value.source.content)
}
is BotScriptSource.URLSource -> {
encodeStringElement(descriptor, 0, TYPE_URL)
encodeStringElement(descriptor, 1, value.source.url.toString())
}
else -> {
encodeStringElement(descriptor, 0, TYPE_UNKNOWN)
encodeStringElement(descriptor, 1, "")
}
}
encodeStringElement(descriptor, 2, value.alias ?: "")
encodeBooleanElement(descriptor, 3, value.autoStart)
encodeStringElement(descriptor, 4, value.source.lang)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ abstract class AbstractBotScript : BotScript {
get() = _stopped

override fun start(): Job {
if (_stopped) throw BotScriptException("Stopped script could not be start again")
if (_active) throw BotScriptException("Script has already been started")
if (_stopped) throw ScriptAlreadyStoppedException("Stopped script could not be start again")
if (_active) throw ScriptAlreadyStartedException()
val job = launch {
onStart()
}
Expand All @@ -36,8 +36,8 @@ abstract class AbstractBotScript : BotScript {
abstract suspend fun onStart()

override fun stop(): Job {
if (_stopped) throw BotScriptException("Script has already stopped")
if (!_active) throw BotScriptException("Script has not been started")
if (_stopped) throw ScriptAlreadyStoppedException()
if (!_active) throw ScriptNotYetStartedException()
val job = launch {
onStop()
}
Expand All @@ -50,4 +50,10 @@ abstract class AbstractBotScript : BotScript {
abstract suspend fun onStop()
}

class BotScriptException(message: String) : RuntimeException(message)
open class BotScriptException(message: String) : RuntimeException(message)

class ScriptAlreadyStoppedException(message: String = "Script has already stopped") : BotScriptException(message)

class ScriptNotYetStartedException(message: String = "Script has not been started") : BotScriptException(message)

class ScriptAlreadyStartedException(message: String = "Script has already stopped") : BotScriptException(message)
7 changes: 5 additions & 2 deletions lua-mirai-mcl-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
plugins {
id 'com.github.johnrengelman.shadow' version "5.2.0"
id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.10'
}

dependencies {
implementation project(":lua-mirai-core")
implementation project(":lua-mirai-configuration")
runtimeOnly project(":lua-mirai-adapter-luakt")
compileOnly "net.mamoe:mirai-console:2.0.0"
testImplementation "net.mamoe:mirai-console-terminal:2.0.0"
compileOnly 'net.mamoe:mirai-console:2.10.3'
testImplementation 'net.mamoe:mirai-console-terminal:2.10.3'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2"
}

task createPluginYml(dependsOn: processResources) {
Expand Down
Loading