mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-25 03:30:15 +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.network.component.ComponentKey
|
||||||
import net.mamoe.mirai.internal.spi.EncryptService
|
import net.mamoe.mirai.internal.spi.EncryptService
|
||||||
import net.mamoe.mirai.internal.spi.EncryptServiceContext
|
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.actualCacheDir
|
||||||
import net.mamoe.mirai.internal.utils.workingDirPath
|
import net.mamoe.mirai.internal.utils.workingDirPath
|
||||||
import net.mamoe.mirai.utils.buildTypeSafeMap
|
import net.mamoe.mirai.utils.buildTypeSafeMap
|
||||||
@ -45,9 +44,8 @@ internal class EncryptServiceHolderImpl(
|
|||||||
get() = service0 ?: error("Encrypt Service not available")
|
get() = service0 ?: error("Encrypt Service not available")
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@OptIn(GlobalEncryptServiceUsage::class)
|
EncryptService.factory?.let { globalService ->
|
||||||
EncryptService.instance?.let { globalService ->
|
service0 = globalService.createForBot(
|
||||||
service0 = globalService.attachToBot(
|
|
||||||
EncryptServiceContext(bot.id, buildTypeSafeMap {
|
EncryptServiceContext(bot.id, buildTypeSafeMap {
|
||||||
set(EncryptServiceContext.KEY_BOT_PROTOCOL, bot.configuration.protocol)
|
set(EncryptServiceContext.KEY_BOT_PROTOCOL, bot.configuration.protocol)
|
||||||
set(EncryptServiceContext.KEY_DEVICE_INFO, ssoProcessorContext.device)
|
set(EncryptServiceContext.KEY_DEVICE_INFO, ssoProcessorContext.device)
|
||||||
|
@ -42,15 +42,7 @@ public class EncryptServiceContext @MiraiInternalApi constructor(
|
|||||||
/**
|
/**
|
||||||
* @since 2.15.0
|
* @since 2.15.0
|
||||||
*/
|
*/
|
||||||
public interface EncryptService : BaseService {
|
public interface EncryptService {
|
||||||
/** service per bot */
|
|
||||||
public fun attachToBot(context: EncryptServiceContext, serviceSubScope: CoroutineScope): EncryptService {
|
|
||||||
/* cleanup:
|
|
||||||
serviceSubScope.coroutineContext.job.invokeOnCompletion { }
|
|
||||||
*/
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun initialize(context: EncryptServiceContext)
|
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?
|
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 {
|
public companion object {
|
||||||
private val loader = SpiServiceLoader(EncryptService::class)
|
|
||||||
|
private val loader = SpiServiceLoader(Factory::class)
|
||||||
|
|
||||||
private val warningAlert: Unit by lazy {
|
private val warningAlert: Unit by lazy {
|
||||||
val log = MiraiLogger.Factory.create(EncryptService::class, "EncryptService.alert")
|
val log = MiraiLogger.Factory.create(EncryptService::class, "EncryptService.alert")
|
||||||
@ -113,15 +117,10 @@ public interface EncryptService : BaseService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GlobalEncryptServiceUsage
|
internal val factory: Factory?
|
||||||
internal val instance: EncryptService?
|
|
||||||
get() {
|
get() {
|
||||||
warningAlert
|
warningAlert
|
||||||
return loader.service
|
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