Merge branch 'master' into command

# Conflicts:
#	tools/gradle-plugin/src/main/kotlin/net/mamoe/mirai/console/gradle/VersionConstants.kt
This commit is contained in:
Him188 2020-10-20 14:11:51 +08:00
commit fa43f2689c
27 changed files with 104 additions and 53 deletions

View File

@ -100,8 +100,8 @@ tasks {
})"""
}
.replace(
Regex("""val version: SemVersion = SemVersion.parse\(".*"\)""")
) { """val version: SemVersion = SemVersion.parse("${project.version}")""" }
Regex("""const val versionConst:\s+String\s+=\s+".*"""")
) { """const val versionConst: String = "${project.version}"""" }
)
}
}

View File

@ -43,6 +43,5 @@ public interface MiraiConsoleFrontEndDescription {
/**
* 返回显示在 [MiraiConsole] 启动时的信息
*/
@JvmDefault
public fun render(): String = "Frontend ${name}: version ${version}, provided by $vendor"
}

View File

@ -108,12 +108,10 @@ public interface MiraiConsoleImplementation : CoroutineScope {
@JvmSynthetic
@JvmDefault
public override suspend fun sendMessage(message: Message): Unit =
withContext(Dispatchers.IO) { sendMessageJ(message) }
@JvmSynthetic
@JvmDefault
public override suspend fun sendMessage(message: String): Unit =
withContext(Dispatchers.IO) { sendMessageJ(message) }
}

View File

@ -170,7 +170,6 @@ public interface CommandSender : CoroutineScope, Permittee {
*
* 对于 [MemberCommandSender], 这个函数总是发送给所在群
*/
@JvmDefault
@JvmBlockingBridge
public suspend fun sendMessage(message: String): MessageReceipt<Contact>?

View File

@ -77,7 +77,6 @@ public interface CommandValueArgumentParser<out T : Any> {
* @see CommandArgumentParserException
*/
@Throws(CommandArgumentParserException::class)
@JvmDefault
public fun parse(raw: MessageContent, sender: CommandSender): T = parse(raw.content, sender)
}

View File

@ -41,7 +41,6 @@ public interface PluginDataHolder {
* @see Companion.newPluginDataInstance
* @see KClass.createType
*/
@JvmDefault
public fun <T : PluginData> newPluginDataInstance(type: KType): T =
newPluginDataInstanceUsingReflection<PluginData>(type) as T
@ -64,7 +63,6 @@ public interface AutoSavePluginDataHolder : PluginDataHolder, CoroutineScope {
/**
* 仅支持确切的 [PluginData] 类型
*/
@JvmDefault
public override fun <T : PluginData> newPluginDataInstance(type: KType): T {
val classifier = type.classifier?.cast<KClass<PluginData>>()
require(classifier != null && classifier.java == PluginData::class.java) {

View File

@ -14,8 +14,9 @@ import java.time.Instant
internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants)
@JvmStatic
val buildDate: Instant = Instant.ofEpochSecond(1600663022)
val buildDate: Instant = Instant.ofEpochSecond(1601134282)
const val versionConst: String = "1.0-RC-dev-29"
@JvmStatic
val version: SemVersion = SemVersion("1.0-RC-dev-28")
val version: SemVersion = SemVersion(versionConst)
}

View File

@ -16,6 +16,7 @@ import net.mamoe.mirai.console.permission.AbstractPermitteeId.*
internal fun parseFromStringImpl(string: String): AbstractPermitteeId {
val str = string.trim { it.isWhitespace() }.toLowerCase()
if (str == "*") return AnyContact
if (str == "console") return Console
if (str.isNotEmpty()) {
when (str[0]) {

View File

@ -42,7 +42,7 @@ internal object BuiltInJvmPluginLoaderImpl :
override val dataStorage: PluginDataStorage
get() = MiraiConsoleImplementationBridge.dataStorageForJvmPluginLoader
internal val classLoaders: MutableList<ClassLoader> = mutableListOf()
internal val classLoaders: MutableList<JvmPluginClassLoader> = mutableListOf()
@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // doesn't matter
override fun getPluginDescription(plugin: JvmPlugin): JvmPluginDescription = plugin.description
@ -68,7 +68,7 @@ internal object BuiltInJvmPluginLoaderImpl :
val filePlugins = this.filterNot {
pluginFileToInstanceMap.containsKey(it)
}.associateWith {
URLClassLoader(arrayOf(it.toURI().toURL()), MiraiConsole::class.java.classLoader)
JvmPluginClassLoader(it, MiraiConsole::class.java.classLoader)
}.onEach { (_, classLoader) ->
classLoaders.add(classLoader)
}.asSequence().findAllInstances().onEach {

View File

@ -0,0 +1,27 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*
*/
package net.mamoe.mirai.console.internal.plugin
import java.io.File
import java.net.URL
import java.net.URLClassLoader
import java.util.*
internal class JvmPluginClassLoader(
file: File,
parent: ClassLoader?,
) : URLClassLoader(arrayOf(file.toURI().toURL()), parent) {
//// 只允许插件 getResource 时获取插件自身资源, #205
override fun getResources(name: String?): Enumeration<URL> = findResources(name)
override fun getResource(name: String?): URL? = findResource(name)
// getResourceAsStream 在 URLClassLoader 中通过 getResource 确定资源
// 因此无需 override getResourceAsStream
}

View File

@ -18,7 +18,6 @@ import net.mamoe.mirai.console.extension.PluginComponentStorage
import net.mamoe.mirai.console.internal.data.mkdir
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.permission.PermissionService
import net.mamoe.mirai.console.permission.PermissionService.Companion.allocatePermissionIdForPlugin
import net.mamoe.mirai.console.plugin.Plugin
import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.safeLoader
@ -26,7 +25,6 @@ import net.mamoe.mirai.console.plugin.ResourceContainer.Companion.asResourceCont
import net.mamoe.mirai.console.plugin.jvm.AbstractJvmPlugin
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin.Companion.onLoad
import net.mamoe.mirai.console.plugin.name
import net.mamoe.mirai.console.util.NamedSupervisorJob
import net.mamoe.mirai.utils.MiraiLogger
import java.io.File
@ -50,7 +48,7 @@ internal abstract class JvmPluginInternal(
final override val parentPermission: Permission by lazy {
PermissionService.INSTANCE.register(
PermissionService.INSTANCE.allocatePermissionIdForPlugin(name, "*"),
PermissionService.INSTANCE.allocatePermissionIdForPlugin(this, "*", PermissionService.PluginPermissionIdRequestType.ROOT_PERMISSION),
"The base permission"
)
}

View File

@ -31,6 +31,13 @@ public data class PermissionId(
@ResolveContext(PERMISSION_NAME) public val name: String,
) {
init {
require(!namespace.contains(' ')) {
"' ' is not allowed in namespace"
}
require(!name.contains(' ')) {
"' ' is not allowed in id"
}
require(!namespace.contains(':')) {
"':' is not allowed in namespace"
}

View File

@ -16,6 +16,8 @@ import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.extensions.PermissionServiceProvider
import net.mamoe.mirai.console.internal.permission.checkType
import net.mamoe.mirai.console.permission.Permission.Companion.parentsWithSelf
import net.mamoe.mirai.console.plugin.Plugin
import net.mamoe.mirai.console.plugin.name
import kotlin.reflect.KClass
/**
@ -89,6 +91,13 @@ public interface PermissionService<P : Permission> {
parent: Permission = RootPermission,
): P
/** 为 [Plugin] 分配一个 [PermissionId] */
public fun allocatePermissionIdForPlugin(
plugin: Plugin,
@ResolveContext(COMMAND_NAME) permissionName: String,
reason: PluginPermissionIdRequestType
): PermissionId = allocatePermissionIdForPluginDefaultImplement(plugin, permissionName, reason)
///////////////////////////////////////////////////////////////////////////
/**
@ -116,6 +125,15 @@ public interface PermissionService<P : Permission> {
@Throws(UnsupportedOperationException::class)
public fun cancel(permitteeId: PermitteeId, permission: P, recursive: Boolean)
/** [Plugin] 尝试分配的 [PermissionId] 来源 */
public enum class PluginPermissionIdRequestType {
/** For [Plugin.parentPermission] */
ROOT_PERMISSION,
/** For [Plugin.permissionId] */
PERMISSION_ID
}
public companion object {
internal var instanceField: PermissionService<*>? = null
@ -131,8 +149,14 @@ public interface PermissionService<P : Permission> {
public fun <P : Permission> PermissionService<P>.getOrFail(id: PermissionId): P =
get(id) ?: throw NoSuchElementException("Permission not found: $id")
internal fun PermissionService<*>.allocatePermissionIdForPlugin(pluginName: String, @ResolveContext(COMMAND_NAME) permissionName: String) =
PermissionId("plugin.${pluginName.toLowerCase()}", permissionName.toLowerCase())
internal fun PermissionService<*>.allocatePermissionIdForPluginDefaultImplement(
plugin: Plugin,
@ResolveContext(COMMAND_NAME) permissionName: String,
reason: PluginPermissionIdRequestType
) = PermissionId(
plugin.name.toLowerCase().replace(' ', '.'),
permissionName.toLowerCase()
)
public fun PermissionId.findCorrespondingPermission(): Permission? = INSTANCE[this]

View File

@ -266,13 +266,13 @@ public sealed class AbstractPermitteeId(
/**
* 表示唯一的一个好友
*
* - **直接父标识符**: [ExactUser]
* - **直接父标识符**: [ExactUser], [AnyFriend]
* - **间接父标识符**: [AnyUser], [AnyContact]
* - 字符串表示: "f$id"
*/
public data class ExactFriend(
public val id: Long,
) : AbstractPermitteeId(ExactUser(id)) {
) : AbstractPermitteeId(ExactUser(id), AnyFriend) {
override fun asString(): String = "f$id"
}
@ -304,7 +304,7 @@ public sealed class AbstractPermitteeId(
* 表示唯一的一个 *在临时会话发送消息的* [群成员][Member]
*
* - **直接父标识符**: [ExactMember]
* - **间接父标识符**: [AnyUser], [AnyMember], [ExactUser], [AnyContact]
* - **间接父标识符**: [AnyUser], [AnyMember], [ExactUser], [AnyContact], [AnyMemberFromAnyGroup]
* - 字符串表示: "t$groupId.$memberId"
*/
public data class ExactTemp(

View File

@ -41,28 +41,24 @@ public interface PluginFileExtensions {
* 从数据目录获取一个文件.
* @see dataFolderPath
*/
@JvmDefault
public fun resolveDataFile(relativePath: String): File = dataFolderPath.resolve(relativePath).toFile()
/**
* 从数据目录获取一个文件.
* @see dataFolderPath
*/
@JvmDefault
public fun resolveDataPath(relativePath: String): Path = dataFolderPath.resolve(relativePath)
/**
* 从数据目录获取一个文件.
* @see dataFolderPath
*/
@JvmDefault
public fun resolveDataFile(relativePath: Path): File = dataFolderPath.resolve(relativePath).toFile()
/**
* 从数据目录获取一个文件路径.
* @see dataFolderPath
*/
@JvmDefault
public fun resolveDataPath(relativePath: Path): Path = dataFolderPath.resolve(relativePath)
@ -83,27 +79,23 @@ public interface PluginFileExtensions {
* 从配置目录获取一个文件.
* @see configFolderPath
*/
@JvmDefault
public fun resolveConfigFile(relativePath: String): File = configFolderPath.resolve(relativePath).toFile()
/**
* 从配置目录获取一个文件.
* @see configFolderPath
*/
@JvmDefault
public fun resolveConfigPath(relativePath: String): Path = configFolderPath.resolve(relativePath)
/**
* 从配置目录获取一个文件.
* @see configFolderPath
*/
@JvmDefault
public fun resolveConfigFile(relativePath: Path): File = configFolderPath.resolve(relativePath).toFile()
/**
* 从配置目录获取一个文件路径.
* @see configFolderPath
*/
@JvmDefault
public fun resolveConfigPath(relativePath: Path): Path = configFolderPath.resolve(relativePath)
}

View File

@ -37,7 +37,6 @@ public interface ResourceContainer {
*
* @return 资源文件内容. 在未找到文件时返回 `null`.
*/
@JvmDefault
public fun getResource(path: String): String? = getResource(path, Charsets.UTF_8)
/**
@ -45,7 +44,6 @@ public interface ResourceContainer {
*
* @return 资源文件内容. 在未找到文件时返回 `null`.
*/
@JvmDefault
public fun getResource(path: String, charset: Charset): String? =
this.getResourceAsStream(path)?.use(InputStream::readBytes)?.let(::String)

View File

@ -16,6 +16,7 @@ import net.mamoe.mirai.console.data.PluginConfig
import net.mamoe.mirai.console.data.PluginData
import net.mamoe.mirai.console.internal.plugin.JvmPluginInternal
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.permission.PermissionService
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.utils.minutesToMillis
import net.mamoe.mirai.utils.secondsToMillis
@ -37,7 +38,8 @@ public abstract class AbstractJvmPlugin @JvmOverloads constructor(
public final override val loader: JvmPluginLoader get() = super<JvmPluginInternal>.loader
public final override fun permissionId(name: String): PermissionId = PermissionId(description.id, name)
public final override fun permissionId(name: String): PermissionId =
PermissionService.INSTANCE.allocatePermissionIdForPlugin(this, name, PermissionService.PluginPermissionIdRequestType.PERMISSION_ID)
/**
* 重载 [PluginData]

View File

@ -96,7 +96,7 @@ import kotlin.internal.LowPriorityInOverloadResolution
*/
public interface MessageScope {
/**
* 如果此 [MessageScope], 仅包含一个消息对象, [realTarget] 指向这个对象.
* 如果此 [MessageScope] 仅包含一个消息对象, [realTarget] 指向这个对象. 否则 [realTarget] `null`.
*
* 对于 [CommandSender] 作为 [MessageScope], [realTarget] 总是指令执行者 [User], [CommandSender.user]
*
@ -116,7 +116,6 @@ public interface MessageScope {
/**
* 立刻以此发送消息给所有在此 [MessageScope] 下的消息对象
*/
@JvmDefault
@JvmBlockingBridge
public suspend fun sendMessage(message: String)
}

View File

@ -36,8 +36,8 @@ class MiraiConsoleBuildPlugin : Plugin<Project> {
attributes(
"Manifest-Version" to "1",
"Implementation-Vendor" to "Mamoe Technologies",
"Implementation-Title" to target.name.toString(),
"Implementation-Version" to target.version.toString() + "+" + gitVersion
"Implementation-Title" to target.name,
"Implementation-Version" to target.version.toString() //+ "+" + gitVersion
)
}
@Suppress("UNCHECKED_CAST")
@ -120,15 +120,16 @@ fun Project.findLatestFile(): Pair<String, File> {
} ?: error("cannot find any file to upload")*/
}
/*
val gitVersion: String by lazy {
runCatching {
val exec = Runtime.getRuntime().exec("git rev-parse HEAD")
exec.waitFor()
exec.inputStream.readBytes().toString(Charsets.UTF_8).trim().also {
println("Git commit id: $it")
}
}.onFailure {
} }.onFailure {
it.printStackTrace()
return@lazy "UNKNOWN"
}.getOrThrow()
}
*/

View File

@ -9,7 +9,7 @@
object Versions {
const val core = "1.3.0"
const val console = "1.0-RC-dev-29"
const val console = "1.0-RC-dev-30"
const val consoleGraphical = "0.0.7"
const val consoleTerminal = console
@ -26,6 +26,6 @@ object Versions {
const val bintray = "1.8.5"
const val blockingBridge = "1.0.5"
const val blockingBridge = "1.1.0"
const val yamlkt = "0.5.3"
}

View File

@ -28,6 +28,8 @@ console 由后端和前端一起工作. 使用时必须选择一个前端.
## 配置项目
请选择以下三种方法之一。
### 使用模板项目
Mirai 鼓励插件开发者将自己的作品开源,并为此提供了模板项目。

View File

@ -151,7 +151,9 @@ val terminal: Terminal = run {
private object ConsoleFrontEndDescImpl : MiraiConsoleFrontEndDescription {
override val name: String get() = "Terminal"
override val vendor: String get() = "Mamoe Technologies"
override val version: SemVersion = net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.version
// net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.version
// is console's version not frontend's version
override val version: SemVersion = SemVersion(net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.versionConst)
}
private val ANSI_RESET = Ansi().reset().toString()

View File

@ -59,7 +59,8 @@ object MiraiConsoleTerminalLoader {
@ConsoleTerminalExperimentalApi
fun printHelpMessage() {
val help = listOf(
"" to "Mirai-Console[Terminal FrontEnd] v" + kotlin.runCatching {
"" to "Mirai-Console[Terminal FrontEnd] v" + net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.versionConst,
"" to " [ BackEnd] v" + kotlin.runCatching {
net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.version
}.getOrElse { "<unknown>" },
"" to "",
@ -171,14 +172,18 @@ internal fun overrideSTD() {
PrintStream(
BufferedOutputStream(
logger = DefaultLogger("stdout").run { ({ line: String? -> info(line) }) }
)
),
false,
"UTF-8"
)
)
System.setErr(
PrintStream(
BufferedOutputStream(
logger = DefaultLogger("stderr").run { ({ line: String? -> warning(line) }) }
)
),
false,
"UTF-8"
)
)
}

View File

@ -10,6 +10,6 @@
package net.mamoe.mirai.console.gradle
internal object VersionConstants {
const val CONSOLE_VERSION = "1.0-RC-dev-29" // value is written here automatically during build
const val CONSOLE_VERSION = "1.0-RC-dev-30" // value is written here automatically during build
const val CORE_VERSION = "1.3.0" // value is written here automatically during build
}

View File

@ -52,7 +52,7 @@ tasks.withType<org.jetbrains.intellij.tasks.PatchPluginXmlTask> {
sinceBuild("193.*")
untilBuild("205.*")
pluginDescription("""
Plugin development support for <a href='https://github.com/mamoe/mirai-console'>Mirai Conosle</a>
Plugin development support for <a href='https://github.com/mamoe/mirai-console'>Mirai Console</a>
<h3>Features</h3>
<ul>

View File

@ -22,7 +22,7 @@ dependencies {
compileOnly(kotlin("stdlib-jdk8"))
val core = "1.3.0"
val console = "1.0-RC-dev-4"
val console = "1.0-RC-dev-28"
compileOnly("net.mamoe:mirai-console:$console")
compileOnly("net.mamoe:mirai-core:$core")

View File

@ -19,7 +19,6 @@ import net.mamoe.mirai.console.compiler.common.resolve.findParent
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
@ -98,7 +97,7 @@ fun KtDeclaration.resolveAllCallsWithElement(bindingContext: BindingContext): Se
.filterIsInstance<KtCallExpression>()
.mapNotNull {
val callee = it.calleeExpression ?: return@mapNotNull null
val resolved = callee.getResolvedCallOrResolveToCall(bindingContext) ?: return@mapNotNull null
val resolved = callee.getResolvedCall(bindingContext) ?: return@mapNotNull null
resolved to it
}
@ -128,7 +127,7 @@ fun KtElement?.getResolvedCallOrResolveToCall(
context: BindingContext,
bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL,
): ResolvedCall<out CallableDescriptor>? {
return this?.getCall(context)?.getResolvedCall(context) ?: this?.resolveToCall(bodyResolveMode)
return this?.getCall(context)?.getResolvedCall(context)// ?: this?.resolveToCall(bodyResolveMode)
}
val ResolvedCall<out CallableDescriptor>.valueParameters: List<ValueParameterDescriptor> get() = this.resultingDescriptor.valueParameters