From a9ca81562741bd5099028d6596de2c0c57b28d41 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Wed, 3 Feb 2021 15:32:00 +0800 Subject: [PATCH] Fix device info coping; fix #281 --- backend/mirai-console/src/MiraiConsole.kt | 29 +++++++++++++++---- .../MiraiConsoleImplementationBridge.kt | 15 ---------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/backend/mirai-console/src/MiraiConsole.kt b/backend/mirai-console/src/MiraiConsole.kt index b1bcb6d18..a00edbc29 100644 --- a/backend/mirai-console/src/MiraiConsole.kt +++ b/backend/mirai-console/src/MiraiConsole.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 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 through the following link. + * 此源代码的使用受 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 + * https://github.com/mamoe/mirai/blob/master/LICENSE */ @file:Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION", "unused") @@ -32,6 +32,7 @@ import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScopeContext import net.mamoe.mirai.console.util.SemVersion import net.mamoe.mirai.utils.BotConfiguration import net.mamoe.mirai.utils.MiraiLogger +import net.mamoe.mirai.utils.verbose import java.io.File import java.nio.file.Path import java.time.Instant @@ -139,8 +140,24 @@ public interface MiraiConsole : CoroutineScope { @Suppress("UNREACHABLE_CODE") private fun addBotImpl(id: Long, password: Any, configuration: BotConfiguration.() -> Unit = {}): Bot { var config = BotConfiguration().apply { - workingDir = rootDir - fileBasedDeviceInfo() + + workingDir = MiraiConsole.rootDir + .resolve("bots") + .resolve(id.toString()) + .also { it.mkdirs() } + + mainLogger.verbose { "Bot $id working in $workingDir" } + + // copy root/device.json to bots/id/deviceInfo.json + val deviceInfoInRoot = MiraiConsole.rootDir.resolve("device.json") + val deviceInfoTarget = workingDir.resolve("deviceInfo.json") + if (deviceInfoInRoot.isFile && !deviceInfoTarget.isFile) { + mainLogger.verbose { "Coping $deviceInfoInRoot to $deviceInfoTarget" } + deviceInfoInRoot.copyTo(deviceInfoTarget) + } + + fileBasedDeviceInfo("deviceInfo.json") + redirectNetworkLogToDirectory() this.botLoggerSupplier = { MiraiLogger.create("Bot.${it.id}") diff --git a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index d91bcf0c9..8658ea516 100644 --- a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -231,21 +231,6 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI ) } } - - 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()) }