mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-24 19:20:30 +08:00
Refactor attachToBot to Factory
This commit is contained in:
parent
787ee833cc
commit
fa389f40bb
@ -13,7 +13,6 @@ import net.mamoe.mirai.internal.AbstractBot
|
||||
import net.mamoe.mirai.internal.network.component.ComponentKey
|
||||
import net.mamoe.mirai.internal.spi.EncryptService
|
||||
import net.mamoe.mirai.internal.spi.EncryptServiceContext
|
||||
import net.mamoe.mirai.internal.spi.GlobalEncryptServiceUsage
|
||||
import net.mamoe.mirai.internal.utils.actualCacheDir
|
||||
import net.mamoe.mirai.internal.utils.workingDirPath
|
||||
import net.mamoe.mirai.utils.buildTypeSafeMap
|
||||
@ -45,9 +44,8 @@ internal class EncryptServiceHolderImpl(
|
||||
get() = service0 ?: error("Encrypt Service not available")
|
||||
|
||||
init {
|
||||
@OptIn(GlobalEncryptServiceUsage::class)
|
||||
EncryptService.instance?.let { globalService ->
|
||||
service0 = globalService.attachToBot(
|
||||
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)
|
||||
|
@ -42,15 +42,7 @@ public class EncryptServiceContext @MiraiInternalApi constructor(
|
||||
/**
|
||||
* @since 2.15.0
|
||||
*/
|
||||
public interface EncryptService : BaseService {
|
||||
/** service per bot */
|
||||
public fun attachToBot(context: EncryptServiceContext, serviceSubScope: CoroutineScope): EncryptService {
|
||||
/* cleanup:
|
||||
serviceSubScope.coroutineContext.job.invokeOnCompletion { }
|
||||
*/
|
||||
return this
|
||||
}
|
||||
|
||||
public interface EncryptService {
|
||||
public fun initialize(context: EncryptServiceContext)
|
||||
|
||||
/**
|
||||
@ -91,8 +83,20 @@ public interface EncryptService : BaseService {
|
||||
public suspend fun sendMessage(remark: String, commandName: String, uin: Long, data: ByteArray): ChannelResult?
|
||||
}
|
||||
|
||||
// net.mamoe.mirai.internal.spi.EncryptService$Factory
|
||||
public interface Factory : BaseService {
|
||||
|
||||
/*
|
||||
* cleanup:
|
||||
* serviceSubScope.coroutineContext.job.invokeOnCompletion { }
|
||||
*/
|
||||
public fun createForBot(context: EncryptServiceContext, serviceSubScope: CoroutineScope): EncryptService
|
||||
}
|
||||
|
||||
|
||||
public companion object {
|
||||
private val loader = SpiServiceLoader(EncryptService::class)
|
||||
|
||||
private val loader = SpiServiceLoader(Factory::class)
|
||||
|
||||
private val warningAlert: Unit by lazy {
|
||||
val log = MiraiLogger.Factory.create(EncryptService::class, "EncryptService.alert")
|
||||
@ -113,15 +117,10 @@ public interface EncryptService : BaseService {
|
||||
|
||||
}
|
||||
|
||||
@GlobalEncryptServiceUsage
|
||||
internal val instance: EncryptService?
|
||||
internal val factory: Factory?
|
||||
get() {
|
||||
warningAlert
|
||||
return loader.service
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RequiresOptIn(message = "Global encrypt service used", level = RequiresOptIn.Level.ERROR)
|
||||
internal annotation class GlobalEncryptServiceUsage
|
||||
|
Loading…
Reference in New Issue
Block a user