diff --git a/.changelog/v4.0.0-dev13.md b/.changelog/v4.0.0-dev13.md new file mode 100644 index 000000000..e69de29bb diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d7073db4..dbf782d2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# v4.0.0-dev13 +> [!warning] +> 这是一个尚在开发中的**预览版**,它可能不稳定,可能会频繁变更,且没有可用性保证。 + + +> Release & Pull Notes: [v4.0.0-dev13](https://github.com/simple-robot/simpler-robot/releases/tag/v4.0.0-dev13) + + # v4.0.0-dev12 > [!warning] > 这是一个尚在开发中的**预览版**,它可能不稳定,可能会频繁变更,且没有可用性保证。 diff --git a/simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/Bot.kt b/simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/Bot.kt index feac53f52..cdb4b23a2 100644 --- a/simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/Bot.kt +++ b/simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/Bot.kt @@ -183,7 +183,7 @@ public interface GuildRelation { * * 如果实现者不支持也可能始终得到 `null`。 */ - @ST(blockingBaseName = "getGuild", blockingSuffix = "", asyncBaseName = "getGuild") + @ST(blockingBaseName = "getGuild", blockingSuffix = "", asyncBaseName = "getGuild", reserveBaseName = "getGuild") public suspend fun guild(id: ID): Guild? /** @@ -214,7 +214,7 @@ public interface GroupRelation { * * 如果实现者不支持也可能始终得到 `null`。 */ - @ST(blockingBaseName = "getGroup", blockingSuffix = "", asyncBaseName = "getGroup") + @ST(blockingBaseName = "getGroup", blockingSuffix = "", asyncBaseName = "getGroup", reserveBaseName = "getGroup") public suspend fun group(id: ID): ChatGroup? /** @@ -247,7 +247,7 @@ public interface ContactRelation { * * 如果实现者不支持也可能始终得到 `null`。 */ - @ST(blockingBaseName = "getContact", blockingSuffix = "", asyncBaseName = "getContact") + @ST(blockingBaseName = "getContact", blockingSuffix = "", asyncBaseName = "getContact", reserveBaseName = "getContact") public suspend fun contact(id: ID): Contact? /** diff --git a/simbot-api/src/commonMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.kt b/simbot-api/src/commonMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.kt index 8d9ea75db..d8e11cb74 100644 --- a/simbot-api/src/commonMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.kt +++ b/simbot-api/src/commonMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.kt @@ -95,24 +95,24 @@ internal class ProviderComponentFactory( * 添加一个用于获取 [ComponentFactoryProvider] 的函数。 * 这是用于兼容在非 `JVM` 平台下没有 `ServiceLoader` 的方案, * 在 `JVM` 中应直接使用 `ServiceLoader` 加载 SPI 的方式 - * 但是如果强行使用 [addProvider] 添加结果, + * 但是如果强行使用 [addComponentFactoryProvider] 添加结果, * [loadComponentProviders] 也还是会得到这些结果的。 */ -public fun addProvider(providerCreator: () -> ComponentFactoryProvider<*>) { +public fun addComponentFactoryProvider(providerCreator: () -> ComponentFactoryProvider<*>) { Services.addProvider>(providerCreator) } /** - * 清理所有通过 [addProvider] 添加的 provider 构建器。 + * 清理所有通过 [addComponentFactoryProvider] 添加的 provider 构建器。 */ -public fun clearProviders() { +public fun clearComponentFactoryProviders() { Services.clearProviders>() } /** * 尝试自动加载环境中可获取的所有 [ComponentFactoryProvider] 实例。 * 在 `JVM` 平台下通过 `ServiceLoader` 加载 [ComponentFactoryProvider] 并得到结果, - * 而在其他平台则会得到预先从 [addProvider] 中添加的所有函数构建出来的结果。 + * 而在其他平台则会得到预先从 [addComponentFactoryProvider] 中添加的所有函数构建出来的结果。 * */ public expect fun loadComponentProviders(): Sequence> diff --git a/simbot-api/src/commonMain/kotlin/love/forte/simbot/definition/ChatRoom.kt b/simbot-api/src/commonMain/kotlin/love/forte/simbot/definition/ChatRoom.kt index abc7c4fff..4fdc4e6de 100644 --- a/simbot-api/src/commonMain/kotlin/love/forte/simbot/definition/ChatRoom.kt +++ b/simbot-api/src/commonMain/kotlin/love/forte/simbot/definition/ChatRoom.kt @@ -92,7 +92,7 @@ public interface Guild : Organization { * 根据ID获取一个指定的频道。 * 如果找不到则会得到 `null`。 */ - @ST(blockingBaseName = "getChannel", blockingSuffix = "", asyncBaseName = "getChannel") + @ST(blockingBaseName = "getChannel", blockingSuffix = "", asyncBaseName = "getChannel", reserveBaseName = "getChannel") public suspend fun channel(id: ID): Channel? /** @@ -104,7 +104,7 @@ public interface Guild : Organization { * 根据ID获取一个指定的聊天频道。 * 如果找不到则会得到 `null`。 */ - @ST(blockingBaseName = "getChatChannel", blockingSuffix = "", asyncBaseName = "getChatChannel") + @ST(blockingBaseName = "getChatChannel", blockingSuffix = "", asyncBaseName = "getChatChannel", reserveBaseName = "getChatChannel") public suspend fun chatChannel(id: ID): ChatChannel? /** diff --git a/simbot-api/src/commonMain/kotlin/love/forte/simbot/definition/Organization.kt b/simbot-api/src/commonMain/kotlin/love/forte/simbot/definition/Organization.kt index 90ec7f90a..0f8bd8948 100644 --- a/simbot-api/src/commonMain/kotlin/love/forte/simbot/definition/Organization.kt +++ b/simbot-api/src/commonMain/kotlin/love/forte/simbot/definition/Organization.kt @@ -59,7 +59,7 @@ public interface Organization : Actor { * * @throws Exception 可能产生任何异常 */ - @ST(blockingBaseName = "getMember", blockingSuffix = "", asyncBaseName = "getMember") + @ST(blockingBaseName = "getMember", blockingSuffix = "", asyncBaseName = "getMember", reserveBaseName = "getMember") public suspend fun member(id: ID): Member? /** diff --git a/simbot-api/src/commonMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.kt b/simbot-api/src/commonMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.kt index 821a788cd..b6c104644 100644 --- a/simbot-api/src/commonMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.kt +++ b/simbot-api/src/commonMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.kt @@ -29,7 +29,7 @@ package love.forte.simbot.plugin import love.forte.simbot.common.function.ConfigurerFunction import love.forte.simbot.common.function.invokeWith import love.forte.simbot.common.services.Services -import love.forte.simbot.component.addProvider +import love.forte.simbot.component.addComponentFactoryProvider import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName @@ -93,24 +93,24 @@ public interface PluginFactoryConfigurerProvider { * 添加一个用于获取 [PluginFactoryProvider] 的函数。 * 这是用于兼容在非 `JVM` 平台下没有 `ServiceLoader` 的方案, * 在 `JVM` 中应直接使用 `ServiceLoader` 加载 SPI 的方式, - * 但是如果使用 [addProvider] 强行添加结果,[loadPluginProviders] + * 但是如果使用 [addComponentFactoryProvider] 强行添加结果,[loadPluginProviders] * 也还是会得到这些结果的。 */ -public fun addProvider(providerCreator: () -> PluginFactoryProvider<*>) { +public fun addPluginFactoryProvider(providerCreator: () -> PluginFactoryProvider<*>) { Services.addProvider>(providerCreator) } /** - * 清理所有通过 [addProvider] 添加的 provider 构建器。 + * 清理所有通过 [addComponentFactoryProvider] 添加的 provider 构建器。 */ -public fun clearProviders() { +public fun clearPluginFactoryProviders() { Services.clearProviders>() } /** * 尝试自动加载环境中可获取的所有 [PluginFactoryProvider] 实例。 * 在 `JVM` 平台下通过 `ServiceLoader` 加载 [PluginFactoryProvider] 并得到结果, - * 而在其他平台则会得到预先从 [addProvider] 中添加的所有函数构建出来的结果。 + * 而在其他平台则会得到预先从 [addComponentFactoryProvider] 中添加的所有函数构建出来的结果。 */ public expect fun loadPluginProviders(): Sequence> diff --git a/simbot-api/src/commonTest/kotlin/ComponentFactoryProviderTests.kt b/simbot-api/src/commonTest/kotlin/ComponentFactoryProviderTests.kt index 342600d42..a5e085e7a 100644 --- a/simbot-api/src/commonTest/kotlin/ComponentFactoryProviderTests.kt +++ b/simbot-api/src/commonTest/kotlin/ComponentFactoryProviderTests.kt @@ -77,14 +77,14 @@ class ComponentFactoryProviderTests { @Test fun addAndGet() { - addProvider { FacP() } - addProvider { FacP() } - addProvider { FacP() } + addComponentFactoryProvider { FacP() } + addComponentFactoryProvider { FacP() } + addComponentFactoryProvider { FacP() } val list = loadComponentProviders().toList() assertEquals(3, list.size) - clearProviders() + clearComponentFactoryProviders() } @Test @@ -93,17 +93,17 @@ class ComponentFactoryProviderTests { withContext(Dispatchers.Default) { launch { repeat(100) { - addProvider { FacP() } + addComponentFactoryProvider { FacP() } } } launch { repeat(100) { - addProvider { FacP() } + addComponentFactoryProvider { FacP() } } } launch { repeat(100) { - addProvider { FacP() } + addComponentFactoryProvider { FacP() } } } } @@ -111,7 +111,7 @@ class ComponentFactoryProviderTests { val list = loadComponentProviders().toList() assertEquals(300, list.size) - clearProviders() + clearComponentFactoryProviders() } } diff --git a/simbot-api/src/jsMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.js.kt b/simbot-api/src/jsMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.js.kt index 9370adf5f..82687a2a3 100644 --- a/simbot-api/src/jsMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.js.kt +++ b/simbot-api/src/jsMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.js.kt @@ -26,7 +26,7 @@ package love.forte.simbot.component import love.forte.simbot.common.services.Services /** - * 获取通过 [addProvider] 添加的内容的副本序列。 + * 获取通过 [addComponentFactoryProvider] 添加的内容的副本序列。 */ public actual fun loadComponentProviders(): Sequence> = Services.loadProviders>().map { it() } diff --git a/simbot-api/src/jsMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.js.kt b/simbot-api/src/jsMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.js.kt index 5764c5be3..3ab82bcf6 100644 --- a/simbot-api/src/jsMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.js.kt +++ b/simbot-api/src/jsMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.js.kt @@ -24,10 +24,10 @@ package love.forte.simbot.plugin import love.forte.simbot.common.services.Services -import love.forte.simbot.component.addProvider +import love.forte.simbot.component.addComponentFactoryProvider /** - * 加载所有通过 [addProvider] 添加的函数构建出来的 [PluginFactoryProvider] 实例。 + * 加载所有通过 [addComponentFactoryProvider] 添加的函数构建出来的 [PluginFactoryProvider] 实例。 */ public actual fun loadPluginProviders(): Sequence> = Services.loadProviders>().map { it() } diff --git a/simbot-api/src/nativeMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.native.kt b/simbot-api/src/nativeMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.native.kt index 9370adf5f..82687a2a3 100644 --- a/simbot-api/src/nativeMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.native.kt +++ b/simbot-api/src/nativeMain/kotlin/love/forte/simbot/component/ComponentFactoryProvider.native.kt @@ -26,7 +26,7 @@ package love.forte.simbot.component import love.forte.simbot.common.services.Services /** - * 获取通过 [addProvider] 添加的内容的副本序列。 + * 获取通过 [addComponentFactoryProvider] 添加的内容的副本序列。 */ public actual fun loadComponentProviders(): Sequence> = Services.loadProviders>().map { it() } diff --git a/simbot-api/src/nativeMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.native.kt b/simbot-api/src/nativeMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.native.kt index 5764c5be3..3ab82bcf6 100644 --- a/simbot-api/src/nativeMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.native.kt +++ b/simbot-api/src/nativeMain/kotlin/love/forte/simbot/plugin/PluginFactoryProvider.native.kt @@ -24,10 +24,10 @@ package love.forte.simbot.plugin import love.forte.simbot.common.services.Services -import love.forte.simbot.component.addProvider +import love.forte.simbot.component.addComponentFactoryProvider /** - * 加载所有通过 [addProvider] 添加的函数构建出来的 [PluginFactoryProvider] 实例。 + * 加载所有通过 [addComponentFactoryProvider] 添加的函数构建出来的 [PluginFactoryProvider] 实例。 */ public actual fun loadPluginProviders(): Sequence> = Services.loadProviders>().map { it() } diff --git a/simbot-commons/simbot-common-core/src/nativeMain/kotlin/love/forte/simbot/common/services/Services.native.kt b/simbot-commons/simbot-common-core/src/nativeMain/kotlin/love/forte/simbot/common/services/Services.native.kt index 677a9981c..5d15425eb 100644 --- a/simbot-commons/simbot-common-core/src/nativeMain/kotlin/love/forte/simbot/common/services/Services.native.kt +++ b/simbot-commons/simbot-common-core/src/nativeMain/kotlin/love/forte/simbot/common/services/Services.native.kt @@ -61,7 +61,7 @@ internal actual fun loadProvidersInternal(type: KClass): Sequence<( return synchronized(lock) { globalProviderCreators[type]?.toList() ?.asSequence() - ?.map { provider -> { type.cast(provider) } } + ?.map { provider -> { type.cast(provider()) } } ?: emptySequence() } } diff --git a/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/EnableSimbot.kt b/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/EnableSimbot.kt index ecabc7382..3f519555d 100644 --- a/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/EnableSimbot.kt +++ b/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/EnableSimbot.kt @@ -31,7 +31,48 @@ import love.forte.simbot.spring.configuration.config.DefaultSimbotApplicationCon import love.forte.simbot.spring.configuration.listener.SimbotEventListenerFunctionProcessor import org.springframework.context.annotation.Import - +/** + * 启用 simbot 的各项配置。 + * + * 将其标记在你的启动类或某个配置类上。 + * + * Kotlin + * + * ```kotlin + * @EnableSimbot + * @SpringBootApplication + * open class MyApplication { + * // ... + * } + * ``` + * + * ```kotlin + * @EnableSimbot + * @Configuration + * open class MyConfig { + * // ... + * } + * ``` + * + * Java + * + * ```java + * @EnableSimbot + * @SpringBootApplication + * public class MyApplication { + * // ... + * } + * ``` + * + * ```java + * @EnableSimbot + * @Configuration + * public class MyConfig { + * // ... + * } + * ``` + * + */ @Target(AnnotationTarget.CLASS) @Import( SimbotSpringPropertiesConfiguration::class, @@ -40,7 +81,6 @@ import org.springframework.context.annotation.Import DefaultSimbotDispatcherProcessorConfiguration::class, DefaultSimbotComponentInstallProcessorConfiguration::class, DefaultSimbotPluginInstallProcessorConfiguration::class, - DefaultSimbotSpringApplicationLauncherFactoryConfiguration::class, DefaultSimbotEventDispatcherProcessorConfiguration::class, DefaultBinderManagerProvidersConfiguration::class, // listeners directly @@ -56,6 +96,5 @@ import org.springframework.context.annotation.Import // post listeners SimbotEventListenerFunctionProcessor::class, SimbotApplicationRunner::class - ) public annotation class EnableSimbot diff --git a/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/configuration/application/DefaultSimbotApplicationProcessor.kt b/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/configuration/application/DefaultSimbotApplicationProcessor.kt index eeea1d821..03c3428c7 100644 --- a/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/configuration/application/DefaultSimbotApplicationProcessor.kt +++ b/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/configuration/application/DefaultSimbotApplicationProcessor.kt @@ -38,6 +38,7 @@ import love.forte.simbot.spring.common.BotAutoStartOnFailureException import love.forte.simbot.spring.common.BotConfigResourceLoadOnFailureException import love.forte.simbot.spring.common.MismatchConfigurableBotManagerException import love.forte.simbot.spring.common.application.* +import love.forte.simbot.spring.configuration.SimbotEventDispatcherProcessor import love.forte.simbot.spring.configuration.listener.SimbotEventListenerResolver import love.forte.simbot.suspendrunner.runInNoScopeBlocking import org.springframework.beans.factory.annotation.Autowired @@ -63,13 +64,15 @@ public open class DefaultSimbotSpringApplicationProcessorConfiguration { properties: SpringApplicationConfigurationProperties, @Autowired(required = false) eventListenerResolvers: List? = null, @Autowired(required = false) preConfigurer: List? = null, - @Autowired(required = false) postConfigurer: List? = null + @Autowired(required = false) postConfigurer: List? = null, + @Autowired(required = false) eventDispatcherProcessor: SimbotEventDispatcherProcessor, ): DefaultSimbotApplicationProcessor = DefaultSimbotApplicationProcessor( properties = properties, eventListenerResolvers = eventListenerResolvers ?: emptyList(), preConfigurer = preConfigurer ?: emptyList(), postConfigurer = postConfigurer ?: emptyList(), + eventDispatcherProcessor ) public companion object { @@ -91,7 +94,8 @@ public class DefaultSimbotApplicationProcessor( private val properties: SpringApplicationConfigurationProperties, private val eventListenerResolvers: List, private val preConfigurer: List, - private val postConfigurer: List + private val postConfigurer: List, + private val eventDispatcherProcessor: SimbotEventDispatcherProcessor, ) : SimbotApplicationProcessor { override fun process(application: SpringApplication) { preConfigurer.forEach { @@ -114,8 +118,8 @@ public class DefaultSimbotApplicationProcessor( private fun process0(application: SpringApplication) { + eventDispatcherProcessor.process(application.eventDispatcher) loadBots(application) - } private fun loadBots(application: SpringApplication) { diff --git a/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/configuration/listener/SimbotEventListenerFunctionProcessor.kt b/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/configuration/listener/SimbotEventListenerFunctionProcessor.kt index d4d4b68e2..68f03617e 100644 --- a/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/configuration/listener/SimbotEventListenerFunctionProcessor.kt +++ b/simbot-cores/simbot-core-spring-boot-starter/src/main/kotlin/love/forte/simbot/spring/configuration/listener/SimbotEventListenerFunctionProcessor.kt @@ -48,6 +48,7 @@ import kotlin.reflect.KFunction /** * 用于通过 [Application] 注册 [EventListener] 的函数接口。 + * 主要是用于通过 [SimbotEventListenerFunctionProcessor] 生成。 */ public fun interface SimbotEventListenerResolver { public fun resolve(application: Application)