From 685fb2d3d994c67bbfa2ae855381da875eb8ec91 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Mon, 3 Jul 2023 20:33:19 +0800 Subject: [PATCH] [core] Handle UnsupportedOperationException on EncryptService initialize & add special signals --- .../components/EncryptServiceHolder.kt | 30 ++++++++++++------- .../commonMain/kotlin/spi/EncryptService.kt | 3 ++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/network/components/EncryptServiceHolder.kt b/mirai-core/src/commonMain/kotlin/network/components/EncryptServiceHolder.kt index c68f82865..9c57d7400 100644 --- a/mirai-core/src/commonMain/kotlin/network/components/EncryptServiceHolder.kt +++ b/mirai-core/src/commonMain/kotlin/network/components/EncryptServiceHolder.kt @@ -45,17 +45,27 @@ internal class EncryptServiceHolderImpl( init { EncryptService.factory?.let { globalService -> - service0 = globalService.createForBot( - EncryptServiceContext(bot.id, buildTypeSafeMap { - set(EncryptServiceContext.KEY_BOT_PROTOCOL, bot.configuration.protocol) - set(EncryptServiceContext.KEY_DEVICE_INFO, ssoProcessorContext.device) - set(EncryptServiceContext.KEY_BOT_WORKING_DIR, bot.configuration.workingDirPath) - set(EncryptServiceContext.KEY_BOT_CACHING_DIR, bot.configuration.actualCacheDir().absolutePath) - }), + try { + service0 = globalService.createForBot( + EncryptServiceContext(bot.id, buildTypeSafeMap { + set(EncryptServiceContext.KEY_BOT_PROTOCOL, bot.configuration.protocol) + set(EncryptServiceContext.KEY_DEVICE_INFO, ssoProcessorContext.device) + set(EncryptServiceContext.KEY_BOT_WORKING_DIR, bot.configuration.workingDirPath) + set(EncryptServiceContext.KEY_BOT_CACHING_DIR, bot.configuration.actualCacheDir().absolutePath) + }), - bot.childScope(name = "Encrypt Service"), - ) - isAvailable = true + bot.childScope(name = "Encrypt Service"), + ) + + isAvailable = true + } catch (error: UnsupportedOperationException) { + bot.logger.warning("$globalService is not yet supported EncryptService with bot $bot", error) + isAvailable = false + service0 = null + } catch (_: EncryptService.SignalServiceNotAvailable) { + isAvailable = false + service0 = null + } /* catch (error: Throwable) { throw error } */ // crash bot initialize } } } diff --git a/mirai-core/src/commonMain/kotlin/spi/EncryptService.kt b/mirai-core/src/commonMain/kotlin/spi/EncryptService.kt index 8a6938e42..8c705d5f3 100644 --- a/mirai-core/src/commonMain/kotlin/spi/EncryptService.kt +++ b/mirai-core/src/commonMain/kotlin/spi/EncryptService.kt @@ -122,4 +122,7 @@ public interface EncryptService { return loader.service } } + + // special error: no service used + public object SignalServiceNotAvailable : RuntimeException() }