Skip to content

Commit

Permalink
Refactor attachToBot to Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlatemp committed Jul 2, 2023
1 parent 787ee83 commit fa389f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 15 additions & 16 deletions mirai-core/src/commonMain/kotlin/spi/EncryptService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

/**
Expand Down Expand Up @@ -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")
Expand All @@ -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

0 comments on commit fa389f4

Please sign in to comment.