1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 04:50:26 +08:00
This commit is contained in:
Him188 2022-05-27 00:10:52 +01:00
parent a1832a4de7
commit 18a29415a7
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375
5 changed files with 186 additions and 47 deletions
mirai-core-utils/src/nativeMain/kotlin
mirai-core/src/nativeMain/kotlin/utils

View File

@ -11,5 +11,9 @@ package net.mamoe.mirai.utils
internal actual fun hash(e: Throwable): Long {
// Stacktrace analysis not available
return e.hashCode().toLongUnsigned()
var hashCode = 1L
for (stackTraceAddress in e.getStackTraceAddresses()) {
hashCode = (hashCode xor stackTraceAddress).shl(1)
}
return hashCode
}

View File

@ -1,14 +0,0 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.utils
public fun symbolNotFound(name: String): Nothing {
throw IllegalStateException("Symbol '$name' not found.")
}

View File

@ -0,0 +1,66 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("RedundantVisibilityModifier")
package net.mamoe.mirai.utils
import kotlinx.atomicfu.locks.reentrantLock
import kotlinx.atomicfu.locks.withLock
import kotlin.reflect.KClass
public object Services {
private val lock = reentrantLock()
private class Implementation(
val implementationClass: String,
val instance: Lazy<Any>
)
private val registered: MutableMap<String, MutableList<Implementation>> = mutableMapOf()
public fun register(baseClass: String, implementationClass: String, implementation: () -> Any) {
lock.withLock {
registered.getOrPut(baseClass, ::mutableListOf)
.add(Implementation(implementationClass, lazy(implementation)))
}
}
public fun firstImplementationOrNull(baseClass: String): Any? {
lock.withLock {
return registered[baseClass]?.firstOrNull()?.instance?.value
}
}
public fun implementations(baseClass: String): List<Any>? {
lock.withLock {
return registered[baseClass]?.map { it.instance }
}
}
}
@Suppress("UNCHECKED_CAST")
public actual fun <T : Any> loadServiceOrNull(
clazz: KClass<out T>,
fallbackImplementation: String?
): T? =
Services.firstImplementationOrNull(qualifiedNameOrFail(clazz)) as T?
public actual fun <T : Any> loadService(
clazz: KClass<out T>,
fallbackImplementation: String?
): T = loadServiceOrNull(clazz, fallbackImplementation)
?: error("Could not load service '${clazz.qualifiedName ?: clazz}'")
public actual fun <T : Any> loadServices(clazz: KClass<out T>): Sequence<T> =
Services.implementations(qualifiedNameOrFail(clazz))?.asSequence().orEmpty().castUp()
private fun <T : Any> qualifiedNameOrFail(clazz: KClass<out T>) =
clazz.qualifiedName ?: error("Could not find qualifiedName for $clazz")

View File

@ -1,32 +0,0 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("RedundantVisibilityModifier")
package net.mamoe.mirai.utils
import kotlin.reflect.KClass
public actual fun <T : Any> loadServiceOrNull(
clazz: KClass<out T>,
fallbackImplementation: String?
): T? {
TODO("Not yet implemented")
}
public actual fun <T : Any> loadService(
clazz: KClass<out T>,
fallbackImplementation: String?
): T {
TODO("Not yet implemented")
}
public actual fun <T : Any> loadServices(clazz: KClass<out T>): Sequence<T> {
TODO("Not yet implemented")
}

View File

@ -0,0 +1,115 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.internal.utils
import net.mamoe.mirai.internal.event.InternalEventMechanism
import net.mamoe.mirai.utils.Services
internal object MiraiCoreServices {
@OptIn(InternalEventMechanism::class)
fun registerAll() {
Services.register(
"net.mamoe.mirai.event.InternalGlobalEventChannelProvider",
"net.mamoe.mirai.internal.event.GlobalEventChannelProviderImpl"
) { net.mamoe.mirai.internal.event.GlobalEventChannelProviderImpl() }
Services.register(
"net.mamoe.mirai.IMirai",
"net.mamoe.mirai.IMirai"
) { net.mamoe.mirai.internal.MiraiImpl() }
val msgProtocol = "net.mamoe.mirai.internal.message.protocol.MessageProtocol"
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.CustomMessageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.CustomMessageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.FaceProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.FaceProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.FileMessageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.FileMessageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.FlashImageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.FlashImageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.IgnoredMessagesProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.IgnoredMessagesProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.ImageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.ImageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.MarketFaceProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.MarketFaceProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.MusicShareProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.MusicShareProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.PokeMessageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.PokeMessageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.PttMessageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.PttMessageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.QuoteReplyProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.QuoteReplyProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.RichMessageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.RichMessageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.TextProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.TextProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.VipFaceProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.VipFaceProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.ForwardMessageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.ForwardMessageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.LongMessageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.LongMessageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.UnsupportedMessageProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.UnsupportedMessageProtocol() }
Services.register(
msgProtocol,
"net.mamoe.mirai.internal.message.protocol.impl.GeneralMessageSenderProtocol"
) { net.mamoe.mirai.internal.message.protocol.impl.GeneralMessageSenderProtocol() }
Services.register(
"net.mamoe.mirai.message.data.InternalImageProtocol",
"net.mamoe.mirai.internal.message.image.InternalImageProtocolImpl"
) { net.mamoe.mirai.internal.message.image.InternalImageProtocolImpl() }
Services.register(
"net.mamoe.mirai.message.data.OfflineAudio.Factory",
"net.mamoe.mirai.internal.message.data.OfflineAudioFactoryImpl"
) { net.mamoe.mirai.internal.message.data.OfflineAudioFactoryImpl() }
}
}