From 0970343bdbd73c7dab85e2437fc47cdd3e4bf057 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 1 Feb 2021 08:53:26 +0800 Subject: [PATCH] Configure workingDir for bots, support custom file name for deviceInfo, close #249 --- .../MiraiConsoleImplementationBridge.kt | 43 +++++++++++++------ .../internal/data/builtins/AutoLoginConfig.kt | 23 +++++++--- .../test/RunTerminal.kt | 3 +- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index 65d73fb5d..459ff4a8e 100644 --- a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -16,10 +16,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.runBlocking import net.mamoe.mirai.Bot -import net.mamoe.mirai.console.MalformedMiraiConsoleImplementationError -import net.mamoe.mirai.console.MiraiConsole -import net.mamoe.mirai.console.MiraiConsoleFrontEndDescription -import net.mamoe.mirai.console.MiraiConsoleImplementation +import net.mamoe.mirai.console.* import net.mamoe.mirai.console.command.BuiltInCommands import net.mamoe.mirai.console.command.CommandManager import net.mamoe.mirai.console.command.ConsoleCommandSender @@ -29,6 +26,7 @@ import net.mamoe.mirai.console.extensions.PostStartupExtension import net.mamoe.mirai.console.extensions.SingletonExtensionSelector import net.mamoe.mirai.console.internal.command.CommandConfig import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig +import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.ConfigurationKey import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.PasswordKind.MD5 import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.PasswordKind.PLAIN import net.mamoe.mirai.console.internal.data.builtins.ConsoleDataScope @@ -223,17 +221,34 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI fun BotConfiguration.configBot() { mainLogger.info { "Auto-login ${account.account}" } - account.configuration[AutoLoginConfig.Account.ConfigurationKey.protocol] - ?.let { protocol -> - this.protocol = runCatching { - BotConfiguration.MiraiProtocol.valueOf(protocol.toString()) - }.getOrElse { - throw IllegalArgumentException( - "Bad auto-login config value for `protocol` for account $id", - it - ) - } + account.configuration[ConfigurationKey.protocol]?.let { protocol -> + this.protocol = runCatching { + BotConfiguration.MiraiProtocol.valueOf(protocol.toString()) + }.getOrElse { + throw IllegalArgumentException( + "Bad auto-login config value for `protocol` for account $id", + it + ) } + } + + workingDir = MiraiConsole.rootDir + .resolve("bots") + .resolve(id.toString()) + + if (!workingDir.exists() + && workingDir.mkdirs() + && account.configuration[ConfigurationKey.device] == null // no custom device + ) { + // copy root/deviceInfo.json to bots/id/deviceInfo.json + val deviceInfoInRoot = MiraiConsole.rootDir.resolve("deviceInfo.json") + deviceInfoInRoot.copyTo(workingDir.resolve("deviceInfo.json")) + fileBasedDeviceInfo("deviceInfo.json") + } + + account.configuration[ConfigurationKey.device]?.let { device -> + fileBasedDeviceInfo(device.toString()) + } } val bot = when (account.password.kind) { diff --git a/backend/mirai-console/src/internal/data/builtins/AutoLoginConfig.kt b/backend/mirai-console/src/internal/data/builtins/AutoLoginConfig.kt index 7e7924b6a..1648f3d32 100644 --- a/backend/mirai-console/src/internal/data/builtins/AutoLoginConfig.kt +++ b/backend/mirai-console/src/internal/data/builtins/AutoLoginConfig.kt @@ -31,10 +31,13 @@ public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") { @Comment("账号, 现只支持 QQ 数字账号") val account: String, val password: Password, - @Comment(""" + @Comment( + """ 账号配置. 可用配置列表 (注意大小写): "protocol": "ANDROID_PHONE" / "ANDROID_PAD" / "ANDROID_WATCH" - """) + "device": "deviceInfo.json" + """ + ) val configuration: Map = mapOf(), ) { @Serializable @@ -49,6 +52,7 @@ public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") { @Serializable public enum class ConfigurationKey { protocol, + device, ; @@ -76,7 +80,16 @@ public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") { } } - public val accounts: MutableList by value(mutableListOf( - Account("123456", Account.Password(Account.PasswordKind.PLAIN, "pwd"), mapOf(Account.ConfigurationKey.protocol to "ANDROID_PHONE")) - )) + public val accounts: MutableList by value( + mutableListOf( + Account( + account = "123456", + password = Account.Password(Account.PasswordKind.PLAIN, "pwd"), + configuration = mapOf( + Account.ConfigurationKey.protocol to "ANDROID_PHONE", + Account.ConfigurationKey.device to "deviceInfo.json" + ) + ) + ) + ) } \ No newline at end of file diff --git a/frontend/mirai-console-terminal/test/RunTerminal.kt b/frontend/mirai-console-terminal/test/RunTerminal.kt index 901433db3..ab34a8272 100644 --- a/frontend/mirai-console-terminal/test/RunTerminal.kt +++ b/frontend/mirai-console-terminal/test/RunTerminal.kt @@ -22,7 +22,8 @@ fun main() { internal fun configureUserDir() { val projectDir = runCatching { - File(".").resolve("frontend").resolve("mirai-console-terminal") + File(".").resolve("frontend").resolve("mirai-console-terminal").takeIf { it.isDirectory } + ?: File(".").resolve("mirai-console/frontend").resolve("mirai-console-terminal") }.getOrElse { return } if (projectDir.isDirectory) { val run = projectDir.resolve("run")