[Review] Rearrange utilities

This commit is contained in:
Him188 2021-01-06 16:09:44 +08:00
parent 41a34bdf65
commit faece77942
36 changed files with 197 additions and 354 deletions

View File

@ -12,12 +12,12 @@
package net.mamoe.mirai.event package net.mamoe.mirai.event
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import net.mamoe.mirai.event.internal.broadcastInternal import net.mamoe.mirai.internal.event.broadcastInternal
import net.mamoe.mirai.utils.JavaFriendlyAPI import net.mamoe.mirai.utils.JavaFriendlyAPI
import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.internal.runBlocking
/** /**
* 可被监听的类, 可以是任何 class object. * 可被监听的类, 可以是任何 class object.

View File

@ -21,10 +21,10 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.event.Listener.ConcurrencyKind.CONCURRENT import net.mamoe.mirai.event.Listener.ConcurrencyKind.CONCURRENT
import net.mamoe.mirai.event.Listener.ConcurrencyKind.LOCKED import net.mamoe.mirai.event.Listener.ConcurrencyKind.LOCKED
import net.mamoe.mirai.event.events.BotEvent import net.mamoe.mirai.event.events.BotEvent
import net.mamoe.mirai.event.internal.GlobalEventListeners import net.mamoe.mirai.internal.event.GlobalEventListeners
import net.mamoe.mirai.event.internal.Handler import net.mamoe.mirai.internal.event.Handler
import net.mamoe.mirai.event.internal.ListenerRegistry import net.mamoe.mirai.internal.event.ListenerRegistry
import net.mamoe.mirai.event.internal.registerEventHandler import net.mamoe.mirai.internal.event.registerEventHandler
import net.mamoe.mirai.utils.JavaFriendlyAPI import net.mamoe.mirai.utils.JavaFriendlyAPI
import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -17,7 +17,7 @@ package net.mamoe.mirai.event
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.* import net.mamoe.mirai.contact.*
import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.event.internal.* import net.mamoe.mirai.internal.event.*
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.MessageSource.Key.quote import net.mamoe.mirai.message.data.MessageSource.Key.quote

View File

@ -1,28 +0,0 @@
/*
* Copyright 2019-2020 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/master/LICENSE
*/
package net.mamoe.mirai.event.internal
import java.util.concurrent.atomic.AtomicBoolean
@PublishedApi
internal class MiraiAtomicBoolean constructor(initial: Boolean) {
private val delegate: AtomicBoolean = AtomicBoolean(initial)
fun compareAndSet(expect: Boolean, update: Boolean): Boolean {
return delegate.compareAndSet(expect, update)
}
var value: Boolean
get() = delegate.get()
set(value) {
delegate.set(value)
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.event.internal package net.mamoe.mirai.internal.event
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
@ -82,7 +82,7 @@ internal object GlobalEventListeners {
EventPriority.values().forEach { EventPriority.values().forEach {
map[it] = ConcurrentLinkedQueue() map[it] = ConcurrentLinkedQueue()
} }
this.ALL_LEVEL_REGISTRIES = map ALL_LEVEL_REGISTRIES = map
} }
operator fun get(priority: Listener.EventPriority): ConcurrentLinkedQueue<ListenerRegistry> = operator fun get(priority: Listener.EventPriority): ConcurrentLinkedQueue<ListenerRegistry> =

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.event.internal package net.mamoe.mirai.internal.event
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.event.internal package net.mamoe.mirai.internal.event
import net.mamoe.mirai.event.MessageDsl import net.mamoe.mirai.event.MessageDsl
import net.mamoe.mirai.event.MessageListener import net.mamoe.mirai.event.MessageListener

View File

@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.message package net.mamoe.mirai.internal.message
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
@ -20,6 +20,7 @@ import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.overwriteWith import kotlinx.serialization.modules.overwriteWith
import kotlinx.serialization.modules.polymorphic import kotlinx.serialization.modules.polymorphic
import net.mamoe.mirai.Mirai import net.mamoe.mirai.Mirai
import net.mamoe.mirai.message.MessageSerializers
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.MessageChainImpl import net.mamoe.mirai.message.data.MessageChainImpl
import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.MiraiInternalApi

View File

@ -0,0 +1,92 @@
/*
* Copyright 2020 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/master/LICENSE
*/
package net.mamoe.mirai.internal.utils
import net.mamoe.mirai.utils.MiraiLoggerPlatformBase
import org.slf4j.Logger
import java.util.logging.Level
internal class Log4jLogger(private val logger: org.apache.logging.log4j.Logger) : MiraiLoggerPlatformBase() {
override fun verbose0(message: String?, e: Throwable?) {
logger.trace(message, e)
}
override fun debug0(message: String?, e: Throwable?) {
logger.debug(message, e)
}
override fun info0(message: String?, e: Throwable?) {
logger.info(message, e)
}
override fun warning0(message: String?, e: Throwable?) {
logger.warn(message, e)
}
override fun error0(message: String?, e: Throwable?) {
logger.error(message, e)
}
override val identity: String?
get() = logger.name
}
internal class Slf4jLogger(private val logger: Logger) : MiraiLoggerPlatformBase() {
override fun verbose0(message: String?, e: Throwable?) {
logger.trace(message, e)
}
override fun debug0(message: String?, e: Throwable?) {
logger.debug(message, e)
}
override fun info0(message: String?, e: Throwable?) {
logger.info(message, e)
}
override fun warning0(message: String?, e: Throwable?) {
logger.warn(message, e)
}
override fun error0(message: String?, e: Throwable?) {
logger.error(message, e)
}
override val identity: String?
get() = logger.name
}
internal class JdkLogger(private val logger: java.util.logging.Logger) : MiraiLoggerPlatformBase() {
override fun verbose0(message: String?, e: Throwable?) {
logger.log(Level.FINER, message, e)
}
override fun debug0(message: String?, e: Throwable?) {
logger.log(Level.FINEST, message, e)
}
override fun info0(message: String?, e: Throwable?) {
logger.log(Level.INFO, message, e)
}
override fun warning0(message: String?, e: Throwable?) {
logger.log(Level.WARNING, message, e)
}
override fun error0(message: String?, e: Throwable?) {
logger.log(Level.SEVERE, message, e)
}
override val identity: String?
get() = logger.name
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.utils.internal package net.mamoe.mirai.internal.utils
import net.mamoe.mirai.utils.LoginSolver import net.mamoe.mirai.utils.LoginSolver
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger

View File

@ -12,6 +12,7 @@ package net.mamoe.mirai.message
import kotlinx.serialization.ContextualSerializer import kotlinx.serialization.ContextualSerializer
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.modules.* import kotlinx.serialization.modules.*
import net.mamoe.mirai.internal.message.MessageSerializersImpl
import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.SingleMessage import net.mamoe.mirai.message.data.SingleMessage

View File

@ -31,13 +31,13 @@ import net.mamoe.mirai.Mirai
import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.Contact.Companion.sendImage import net.mamoe.mirai.contact.Contact.Companion.sendImage
import net.mamoe.mirai.contact.Contact.Companion.uploadImage import net.mamoe.mirai.contact.Contact.Companion.uploadImage
import net.mamoe.mirai.internal.message.map
import net.mamoe.mirai.internal.message.mapPrimitive
import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.data.Image.Key.IMAGE_ID_REGEX import net.mamoe.mirai.message.data.Image.Key.IMAGE_ID_REGEX
import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_1 import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_1
import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_2 import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_2
import net.mamoe.mirai.message.data.Image.Key.queryUrl import net.mamoe.mirai.message.data.Image.Key.queryUrl
import net.mamoe.mirai.message.map
import net.mamoe.mirai.message.mapPrimitive
import net.mamoe.mirai.utils.ExternalResource import net.mamoe.mirai.utils.ExternalResource
import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage

View File

@ -23,8 +23,8 @@ import net.mamoe.mirai.IMirai
import net.mamoe.mirai.Mirai import net.mamoe.mirai.Mirai
import net.mamoe.mirai.contact.* import net.mamoe.mirai.contact.*
import net.mamoe.mirai.event.events.MessageEvent import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.internal.message.MessageSourceSerializerImpl
import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.MessageSourceSerializerImpl
import net.mamoe.mirai.message.data.MessageSource.Key.isAboutFriend import net.mamoe.mirai.message.data.MessageSource.Key.isAboutFriend
import net.mamoe.mirai.message.data.MessageSource.Key.isAboutGroup import net.mamoe.mirai.message.data.MessageSource.Key.isAboutGroup
import net.mamoe.mirai.message.data.MessageSource.Key.isAboutStranger import net.mamoe.mirai.message.data.MessageSource.Key.isAboutStranger

View File

@ -14,9 +14,6 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.protobuf.ProtoBuf import kotlinx.serialization.protobuf.ProtoBuf
import kotlinx.serialization.protobuf.ProtoNumber import kotlinx.serialization.protobuf.ProtoNumber
import net.mamoe.mirai.utils.internal.getRandomByteArray
import net.mamoe.mirai.utils.internal.getRandomIntString
import net.mamoe.mirai.utils.internal.getRandomString
import java.io.File import java.io.File
@Serializable @Serializable

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -9,9 +9,9 @@
package net.mamoe.mirai.utils package net.mamoe.mirai.utils
import net.mamoe.mirai.utils.internal.logging.JdkLogger import net.mamoe.mirai.internal.utils.JdkLogger
import net.mamoe.mirai.utils.internal.logging.Log4jLogger import net.mamoe.mirai.internal.utils.Log4jLogger
import net.mamoe.mirai.utils.internal.logging.Slf4jLogger import net.mamoe.mirai.internal.utils.Slf4jLogger
public object LoggerAdapters { public object LoggerAdapters {
@JvmStatic @JvmStatic

View File

@ -18,13 +18,13 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.internal.utils.SeleniumLoginSolver
import net.mamoe.mirai.internal.utils.isSliderCaptchaSupportKind
import net.mamoe.mirai.network.LoginFailedException import net.mamoe.mirai.network.LoginFailedException
import net.mamoe.mirai.network.NoStandardInputForCaptchaException import net.mamoe.mirai.network.NoStandardInputForCaptchaException
import net.mamoe.mirai.utils.DeviceInfo.Companion.loadAsDeviceInfo import net.mamoe.mirai.utils.DeviceInfo.Companion.loadAsDeviceInfo
import net.mamoe.mirai.utils.LoginSolver.Companion.Default import net.mamoe.mirai.utils.LoginSolver.Companion.Default
import net.mamoe.mirai.utils.StandardCharImageLoginSolver.Companion.createBlocking import net.mamoe.mirai.utils.StandardCharImageLoginSolver.Companion.createBlocking
import net.mamoe.mirai.utils.internal.SeleniumLoginSolver
import net.mamoe.mirai.utils.internal.isSliderCaptchaSupportKind
import java.awt.Image import java.awt.Image
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import java.io.File import java.io.File

View File

@ -1,40 +0,0 @@
/*
* Copyright 2019-2020 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/master/LICENSE
*/
package net.mamoe.mirai.utils.internal.logging
import net.mamoe.mirai.utils.MiraiLoggerPlatformBase
import java.util.logging.Level
import java.util.logging.Logger
internal class JdkLogger(private val logger: Logger) : MiraiLoggerPlatformBase() {
override fun verbose0(message: String?, e: Throwable?) {
logger.log(Level.FINER, message, e)
}
override fun debug0(message: String?, e: Throwable?) {
logger.log(Level.FINEST, message, e)
}
override fun info0(message: String?, e: Throwable?) {
logger.log(Level.INFO, message, e)
}
override fun warning0(message: String?, e: Throwable?) {
logger.log(Level.WARNING, message, e)
}
override fun error0(message: String?, e: Throwable?) {
logger.log(Level.SEVERE, message, e)
}
override val identity: String?
get() = logger.name
}

View File

@ -1,40 +0,0 @@
/*
* Copyright 2019-2020 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/master/LICENSE
*/
package net.mamoe.mirai.utils.internal.logging
import net.mamoe.mirai.utils.MiraiLoggerPlatformBase
import org.apache.logging.log4j.Logger
internal class Log4jLogger(private val logger: Logger) : MiraiLoggerPlatformBase() {
override fun verbose0(message: String?, e: Throwable?) {
logger.trace(message, e)
}
override fun debug0(message: String?, e: Throwable?) {
logger.debug(message, e)
}
override fun info0(message: String?, e: Throwable?) {
logger.info(message, e)
}
override fun warning0(message: String?, e: Throwable?) {
logger.warn(message, e)
}
override fun error0(message: String?, e: Throwable?) {
logger.error(message, e)
}
override val identity: String?
get() = logger.name
}

View File

@ -1,38 +0,0 @@
/*
* Copyright 2019-2020 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/master/LICENSE
*/
package net.mamoe.mirai.utils.internal.logging
import net.mamoe.mirai.utils.MiraiLoggerPlatformBase
import org.slf4j.Logger
internal class Slf4jLogger(private val logger: Logger) : MiraiLoggerPlatformBase() {
override fun verbose0(message: String?, e: Throwable?) {
logger.trace(message, e)
}
override fun debug0(message: String?, e: Throwable?) {
logger.debug(message, e)
}
override fun info0(message: String?, e: Throwable?) {
logger.info(message, e)
}
override fun warning0(message: String?, e: Throwable?) {
logger.warn(message, e)
}
override fun error0(message: String?, e: Throwable?) {
logger.error(message, e)
}
override val identity: String?
get() = logger.name
}

View File

@ -1,39 +0,0 @@
/*
* Copyright 2019-2020 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/master/LICENSE
*/
package net.mamoe.mirai.utils.internal
import kotlin.reflect.KClass
// Currently we can't share internal code between modules.
@Suppress("DuplicatedCode", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
internal inline fun <R> retryCatching(
n: Int,
except: KClass<out Throwable>? = null,
block: (count: Int, lastException: Throwable?) -> R
): Result<R> {
require(n >= 0) {
"param n for retryCatching must not be negative"
}
var exception: Throwable? = null
repeat(n) {
try {
return Result.success(block(it, exception))
} catch (e: Throwable) {
if (except?.isInstance(e) == true) {
return Result.failure(e)
}
exception?.addSuppressed(e)
exception = e
}
}
return Result.failure(exception!!)
}

View File

@ -1,22 +0,0 @@
/*
* Copyright 2019-2020 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/master/LICENSE
*/
package net.mamoe.mirai.utils.internal
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
/**
* Mirror of `runBlocking` from `kotlinx.serialization` on JVM.
*/
internal expect fun <T> runBlocking(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> T
): T

View File

@ -1,21 +0,0 @@
/*
* Copyright 2019-2020 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/master/LICENSE
*/
package net.mamoe.mirai.utils.internal
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
/**
* Mirror of `runBlocking` from `kotlinx.serialization` on JVM.
*/
internal actual fun <T> runBlocking(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
): T = kotlinx.coroutines.runBlocking(context, block)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -10,10 +10,8 @@
package net.mamoe.mirai.event package net.mamoe.mirai.event
import kotlinx.coroutines.* import kotlinx.coroutines.*
import net.mamoe.mirai.event.* import net.mamoe.mirai.internal.event.GlobalEventListeners
import net.mamoe.mirai.event.internal.GlobalEventListeners
import net.mamoe.mirai.utils.StepUtil import net.mamoe.mirai.utils.StepUtil
import net.mamoe.mirai.utils.internal.runBlocking
import java.util.concurrent.Executor import java.util.concurrent.Executor
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
import kotlin.test.Test import kotlin.test.Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -7,9 +7,9 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.utils.internal package net.mamoe.mirai.utils
import kotlin.jvm.JvmSynthetic
import kotlin.random.Random import kotlin.random.Random
import kotlin.random.nextInt import kotlin.random.nextInt
@ -17,33 +17,29 @@ import kotlin.random.nextInt
/** /**
* 生成长度为 [length], 元素为随机 `0..255` [ByteArray] * 生成长度为 [length], 元素为随机 `0..255` [ByteArray]
*/ */
@JvmSynthetic public fun getRandomByteArray(length: Int): ByteArray = ByteArray(length) { Random.nextInt(0..255).toByte() }
internal fun getRandomByteArray(length: Int): ByteArray = ByteArray(length) { Random.nextInt(0..255).toByte() }
/** /**
* 随机生成长度为 [length] [String]. * 随机生成长度为 [length] [String].
*/ */
@JvmSynthetic public fun getRandomString(length: Int): String =
internal fun getRandomString(length: Int): String =
getRandomString(length, *defaultRanges) getRandomString(length, *defaultRanges)
private val defaultRanges: Array<CharRange> = arrayOf('a'..'z', 'A'..'Z', '0'..'9') private val defaultRanges: Array<CharRange> = arrayOf('a'..'z', 'A'..'Z', '0'..'9')
private val intCharRanges: Array<CharRange> = arrayOf('0'..'9') private val intCharRanges: Array<CharRange> = arrayOf('0'..'9')
/** /**
* 根据所给 [charRange] 随机生成长度为 [length] [String]. * 根据所给 [charRange] 随机生成长度为 [length] [String].
*/ */
@JvmSynthetic public fun getRandomString(length: Int, charRange: CharRange): String =
internal fun getRandomString(length: Int, charRange: CharRange): String =
CharArray(length) { charRange.random() }.concatToString() CharArray(length) { charRange.random() }.concatToString()
/** /**
* 根据所给 [charRanges] 随机生成长度为 [length] [String]. * 根据所给 [charRanges] 随机生成长度为 [length] [String].
*/ */
@JvmSynthetic public fun getRandomString(length: Int, vararg charRanges: CharRange): String =
internal fun getRandomString(length: Int, vararg charRanges: CharRange): String =
CharArray(length) { charRanges[Random.Default.nextInt(0..charRanges.lastIndex)].random() }.concatToString() CharArray(length) { charRanges[Random.Default.nextInt(0..charRanges.lastIndex)].random() }.concatToString()
@JvmSynthetic public fun getRandomIntString(length: Int): String =
internal fun getRandomIntString(length: Int): String =
getRandomString(length, *intCharRanges) getRandomString(length, *intCharRanges)

View File

@ -12,8 +12,62 @@
package net.mamoe.mirai.utils package net.mamoe.mirai.utils
import kotlin.reflect.KClass
public inline fun <reified T> Any?.cast(): T = this as T public inline fun <reified T> Any?.cast(): T = this as T
public inline fun <reified T> Any?.safeCast(): T? = this as? T public inline fun <reified T> Any?.safeCast(): T? = this as? T
public inline fun <reified T> Any?.castOrNull(): T? = this as? T public inline fun <reified T> Any?.castOrNull(): T? = this as? T
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
@kotlin.internal.LowPriorityInOverloadResolution
public inline fun <R> retryCatching(
n: Int,
except: KClass<out Throwable>? = null,
block: (count: Int, lastException: Throwable?) -> R
): Result<R> {
require(n >= 0) {
"param n for retryCatching must not be negative"
}
var exception: Throwable? = null
repeat(n) {
try {
return Result.success(block(it, exception))
} catch (e: Throwable) {
if (except?.isInstance(e) == true) {
return Result.failure(e)
}
exception?.addSuppressed(e)
exception = e
}
}
return Result.failure(exception!!)
}
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
public inline fun <R> retryCatching(
n: Int,
except: KClass<out Throwable>? = null,
block: () -> R
): Result<R> {
require(n >= 0) {
"param n for retryCatching must not be negative"
}
var exception: Throwable? = null
repeat(n) {
try {
return Result.success(block())
} catch (e: Throwable) {
if (except?.isInstance(e) == true) {
return Result.failure(e)
}
exception?.addSuppressed(e)
exception = e
}
}
return Result.failure(exception!!)
}

View File

@ -34,7 +34,6 @@ import net.mamoe.mirai.network.ForceOfflineException
import net.mamoe.mirai.network.LoginFailedException import net.mamoe.mirai.network.LoginFailedException
import net.mamoe.mirai.supervisorJob import net.mamoe.mirai.supervisorJob
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.internal.retryCatching
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlin.time.ExperimentalTime import kotlin.time.ExperimentalTime
import kotlin.time.measureTime import kotlin.time.measureTime

View File

@ -25,7 +25,6 @@ import net.mamoe.mirai.internal.network.protocol.data.proto.SourceMsg
import net.mamoe.mirai.internal.network.protocol.packet.EMPTY_BYTE_ARRAY import net.mamoe.mirai.internal.network.protocol.packet.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.internal.utils._miraiContentToString import net.mamoe.mirai.internal.utils._miraiContentToString
import net.mamoe.mirai.internal.utils.io.serialization.toByteArray import net.mamoe.mirai.internal.utils.io.serialization.toByteArray
import net.mamoe.mirai.message.MessageSourceSerializerImpl
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.encodeToBase64 import net.mamoe.mirai.utils.encodeToBase64
import net.mamoe.mirai.utils.encodeToString import net.mamoe.mirai.utils.encodeToString

View File

@ -18,7 +18,6 @@ import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody
import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm
import net.mamoe.mirai.internal.network.protocol.data.proto.SourceMsg import net.mamoe.mirai.internal.network.protocol.data.proto.SourceMsg
import net.mamoe.mirai.internal.utils.io.serialization.loadAs import net.mamoe.mirai.internal.utils.io.serialization.loadAs
import net.mamoe.mirai.message.MessageSourceSerializerImpl
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSourceKind import net.mamoe.mirai.message.data.MessageSourceKind
import net.mamoe.mirai.message.data.OfflineMessageSource import net.mamoe.mirai.message.data.OfflineMessageSource

View File

@ -25,7 +25,6 @@ import net.mamoe.mirai.internal.network.protocol.data.proto.SourceMsg
import net.mamoe.mirai.internal.network.protocol.packet.chat.receive.OnlinePushPbPushGroupMsg.SendGroupMessageReceipt import net.mamoe.mirai.internal.network.protocol.packet.chat.receive.OnlinePushPbPushGroupMsg.SendGroupMessageReceipt
import net.mamoe.mirai.internal.network.protocol.packet.chat.toLongUnsigned import net.mamoe.mirai.internal.network.protocol.packet.chat.toLongUnsigned
import net.mamoe.mirai.internal.utils.io.serialization.toByteArray import net.mamoe.mirai.internal.utils.io.serialization.toByteArray
import net.mamoe.mirai.message.MessageSourceSerializerImpl
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSource import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.message.data.OnlineMessageSource import net.mamoe.mirai.message.data.OnlineMessageSource

View File

@ -740,7 +740,7 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
if (retry == 0) { if (retry == 0) {
val handler = PacketListener(commandName = commandName, sequenceId = sequenceId) val handler = PacketListener(commandName = commandName, sequenceId = sequenceId)
packetListeners.addLast(handler) packetListeners.add(handler)
try { try {
return doSendAndReceive(handler, delegate, 0) // no need return doSendAndReceive(handler, delegate, 0) // no need
} finally { } finally {
@ -750,7 +750,7 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
val data = this.delegate.readBytes() val data = this.delegate.readBytes()
return retryCatching(retry + 1) { return retryCatching(retry + 1) {
val handler = PacketListener(commandName = commandName, sequenceId = sequenceId) val handler = PacketListener(commandName = commandName, sequenceId = sequenceId)
packetListeners.addLast(handler) packetListeners.add(handler)
try { try {
doSendAndReceive(handler, data, data.size) doSendAndReceive(handler, data, data.size)
} finally { } finally {
@ -761,7 +761,7 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
} }
@PublishedApi @PublishedApi
internal val packetListeners = LockFreeLinkedList<PacketListener>() internal val packetListeners = ConcurrentLinkedQueue<PacketListener>()
@PublishedApi @PublishedApi
internal inner class PacketListener( internal inner class PacketListener(

View File

@ -28,7 +28,6 @@ import net.mamoe.mirai.internal.network.protocol.packet.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.internal.network.protocol.packet.chat.voice.voiceCodec import net.mamoe.mirai.internal.network.protocol.packet.chat.voice.voiceCodec
import net.mamoe.mirai.internal.utils.PlatformSocket import net.mamoe.mirai.internal.utils.PlatformSocket
import net.mamoe.mirai.internal.utils.SocketException import net.mamoe.mirai.internal.utils.SocketException
import net.mamoe.mirai.internal.utils.addSuppressedMirai
import net.mamoe.mirai.internal.utils.io.serialization.readProtoBuf import net.mamoe.mirai.internal.utils.io.serialization.readProtoBuf
import net.mamoe.mirai.internal.utils.io.serialization.toByteArray import net.mamoe.mirai.internal.utils.io.serialization.toByteArray
import net.mamoe.mirai.internal.utils.toIpV4AddressString import net.mamoe.mirai.internal.utils.toIpV4AddressString
@ -314,7 +313,7 @@ internal suspend inline fun List<Pair<Int, Int>>.retryWithServers(
} }
}.recover { }.recover {
if (exception != null) { if (exception != null) {
exception!!.addSuppressedMirai(it) exception!!.addSuppressed(it)
} }
exception = it exception = it
null null

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -7,15 +7,8 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmName("Utils")
@file:JvmMultifileClass
package net.mamoe.mirai.internal.utils package net.mamoe.mirai.internal.utils
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
/** /**
* GUID 来源 * GUID 来源
* *
@ -32,16 +25,19 @@ internal inline class GuidSource private constructor(val id: Long) { // uint act
*/ */
@JvmStatic @JvmStatic
val STUB = GuidSource(0) val STUB = GuidSource(0)
/** /**
* *
*/ */
@JvmStatic @JvmStatic
val NEWLY_GENERATED = GuidSource(17) val NEWLY_GENERATED = GuidSource(17)
/** /**
* 以前没保存但现在生成成功 * 以前没保存但现在生成成功
*/ */
@JvmStatic @JvmStatic
val FROM_STORAGE = GuidSource(1) val FROM_STORAGE = GuidSource(1)
/** /**
* 以前没保存且现在生成失败 * 以前没保存且现在生成失败
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -7,14 +7,8 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmName("Utils")
@file:JvmMultifileClass
package net.mamoe.mirai.internal.utils package net.mamoe.mirai.internal.utils
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
internal inline class MacOrAndroidIdChangeFlag(val value: Long = 0) { internal inline class MacOrAndroidIdChangeFlag(val value: Long = 0) {
fun macChanged(): MacOrAndroidIdChangeFlag = fun macChanged(): MacOrAndroidIdChangeFlag =
MacOrAndroidIdChangeFlag(this.value or 0x1) MacOrAndroidIdChangeFlag(this.value or 0x1)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -7,14 +7,8 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("Utils")
package net.mamoe.mirai.internal.utils package net.mamoe.mirai.internal.utils
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
/** /**
* 要求 [this] 最小为 [min]. * 要求 [this] 最小为 [min].
*/ */

View File

@ -1,44 +0,0 @@
/*
* Copyright 2019-2020 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/master/LICENSE
*/
@file:Suppress("DuplicatedCode")
@file:JvmMultifileClass
@file:JvmName("Utils")
package net.mamoe.mirai.internal.utils
import kotlin.reflect.KClass
@PublishedApi
internal fun Throwable.addSuppressedMirai(e: Throwable) {
if (e === this) return
kotlin.runCatching {
this.addSuppressed(e)
}
}
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
internal inline fun <R> retryCatching(n: Int, except: KClass<out Throwable>? = null, block: () -> R): Result<R> {
require(n > 0) { "param n for retryCatching must not be negative" }
var exception: Throwable? = null
repeat(n) {
try {
return Result.success(block())
} catch (e: Throwable) {
if (except?.isInstance(e) == true) {
return Result.failure(e)
}
exception?.addSuppressedMirai(e)
exception = e
}
}
return Result.failure(exception!!)
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -7,9 +7,6 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmName("Utils")
@file:JvmMultifileClass
package net.mamoe.mirai.internal.utils package net.mamoe.mirai.internal.utils
import net.mamoe.mirai.contact.ContactOrBot import net.mamoe.mirai.contact.ContactOrBot