[core] Move DeviceInfo from jvmBase to common

This commit is contained in:
Him188 2023-08-12 16:10:29 +08:00
parent af89f4d66d
commit 42763fd15c
No known key found for this signature in database
GPG Key ID: 71E7750DD6CA1CE1
2 changed files with 141 additions and 302 deletions

View File

@ -16,10 +16,10 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
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 kotlin.jvm.JvmStatic import java.io.File
import kotlin.jvm.JvmSynthetic
import kotlin.random.Random import kotlin.random.Random
internal const val DeviceInfoConstructorDeprecationMessage = internal const val DeviceInfoConstructorDeprecationMessage =
@ -56,9 +56,11 @@ internal const val DeviceInfoConstructorReplaceWith = "DeviceInfoBuilder.create(
* 表示设备信息 * 表示设备信息
* @see DeviceInfoBuilder * @see DeviceInfoBuilder
*/ */
public expect class DeviceInfo @Serializable(DeviceInfoV1LegacySerializer::class)
public class DeviceInfo
@Deprecated( @Deprecated(
DeviceInfoConstructorDeprecationMessage, level = DeprecationLevel.WARNING, DeviceInfoConstructorDeprecationMessage,
level = DeprecationLevel.WARNING,
replaceWith = ReplaceWith( replaceWith = ReplaceWith(
DeviceInfoConstructorReplaceWith, DeviceInfoConstructorReplaceWith,
"net.mamoe.mirai.utils.DeviceInfoBuilder" "net.mamoe.mirai.utils.DeviceInfoBuilder"
@ -66,85 +68,138 @@ public expect class DeviceInfo
) )
@DeprecatedSinceMirai(warningSince = "2.15") // planned internal @DeprecatedSinceMirai(warningSince = "2.15") // planned internal
public constructor( public constructor(
display: ByteArray, public val display: ByteArray,
product: ByteArray, public val product: ByteArray,
device: ByteArray, public val device: ByteArray,
board: ByteArray, public val board: ByteArray,
brand: ByteArray, public val brand: ByteArray,
model: ByteArray, public val model: ByteArray,
bootloader: ByteArray, public val bootloader: ByteArray,
fingerprint: ByteArray, public val fingerprint: ByteArray,
bootId: ByteArray, public val bootId: ByteArray,
procVersion: ByteArray, public val procVersion: ByteArray,
baseBand: ByteArray, public val baseBand: ByteArray,
version: Version, public val version: Version,
simInfo: ByteArray, public val simInfo: ByteArray,
osType: ByteArray, public val osType: ByteArray,
macAddress: ByteArray, public val macAddress: ByteArray,
wifiBSSID: ByteArray, public val wifiBSSID: ByteArray,
wifiSSID: ByteArray, public val wifiSSID: ByteArray,
imsiMd5: ByteArray, public val imsiMd5: ByteArray,
imei: String, public val imei: String,
apn: ByteArray, public val apn: ByteArray,
androidId: ByteArray, public val androidId: ByteArray,
) { ) {
public val display: ByteArray @Deprecated(
public val product: ByteArray DeviceInfoConstructorDeprecationMessage,
public val device: ByteArray replaceWith = ReplaceWith(
public val board: ByteArray "net.mamoe.mirai.utils.DeviceInfo(display, product, device, board, brand, model, " +
public val brand: ByteArray "bootloader, fingerprint, bootId, procVersion, baseBand, version, simInfo, osType, " +
public val model: ByteArray "macAddress, wifiBSSID, wifiSSID, imsiMd5, imei, apn, androidId)"
public val bootloader: ByteArray ),
public val fingerprint: ByteArray level = DeprecationLevel.WARNING
public val bootId: ByteArray )
public val procVersion: ByteArray @DeprecatedSinceMirai(warningSince = "2.15")
public val baseBand: ByteArray @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public val version: Version public constructor(
public val simInfo: ByteArray display: ByteArray,
public val osType: ByteArray product: ByteArray,
public val macAddress: ByteArray device: ByteArray,
public val wifiBSSID: ByteArray board: ByteArray,
public val wifiSSID: ByteArray brand: ByteArray,
public val imsiMd5: ByteArray model: ByteArray,
public val imei: String bootloader: ByteArray,
public val apn: ByteArray fingerprint: ByteArray,
public val androidId: ByteArray bootId: ByteArray,
procVersion: ByteArray,
baseBand: ByteArray,
version: Version,
simInfo: ByteArray,
osType: ByteArray,
macAddress: ByteArray,
wifiBSSID: ByteArray,
wifiSSID: ByteArray,
imsiMd5: ByteArray,
imei: String,
apn: ByteArray
) : this(
display, product, device, board, brand, model, bootloader,
fingerprint, bootId, procVersion, baseBand, version, simInfo,
osType, macAddress, wifiBSSID, wifiSSID, imsiMd5, imei, apn,
androidId = display
)
public val ipAddress: ByteArray public val ipAddress: ByteArray get() = byteArrayOf(192.toByte(), 168.toByte(), 1, 123)
init {
require(imsiMd5.size == 16) { "Bad `imsiMd5.size`. Required 16, given ${imsiMd5.size}." }
}
@Transient @Transient
@MiraiInternalApi @MiraiInternalApi
public val guid: ByteArray public val guid: ByteArray = generateGuid(androidId, macAddress)
// @Serializable: use DeviceInfoVersionSerializer in commonMain. @Serializable
public class Version( public class Version(
incremental: ByteArray = "5891938".toByteArray(), public val incremental: ByteArray = "5891938".toByteArray(),
release: ByteArray = "10".toByteArray(), public val release: ByteArray = "10".toByteArray(),
codename: ByteArray = "REL".toByteArray(), public val codename: ByteArray = "REL".toByteArray(),
sdk: Int = 29 public val sdk: Int = 29
) { ) {
public val incremental: ByteArray /**
public val release: ByteArray * @since 2.9
public val codename: ByteArray */
public val sdk: Int override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Version) return false
if (!incremental.contentEquals(other.incremental)) return false
if (!release.contentEquals(other.release)) return false
if (!codename.contentEquals(other.codename)) return false
return sdk == other.sdk
}
/** /**
* @since 2.9 * @since 2.9
*/ */
override fun equals(other: Any?): Boolean override fun hashCode(): Int {
var result = incremental.contentHashCode()
/** result = 31 * result + release.contentHashCode()
* @since 2.9 result = 31 * result + codename.contentHashCode()
*/ result = 31 * result + sdk
override fun hashCode(): Int return result
internal companion object {
fun serializer(): KSerializer<Version>
} }
} }
public companion object { public companion object {
internal val logger: MiraiLogger internal val logger = MiraiLogger.Factory.create(DeviceInfo::class, "DeviceInfo")
/**
* 加载一个设备信息. 若文件不存在或为空则随机并创建一个设备信息保存.
*/
@JvmOverloads
@JvmStatic
@JvmName("from")
public fun File.loadAsDeviceInfo(
json: Json = DeviceInfoManager.format
): DeviceInfo {
if (!this.exists() || this.length() == 0L) {
return random().also {
this.writeText(DeviceInfoManager.serialize(it, json))
}
}
return DeviceInfoManager.deserialize(this.readText(), json) upg@{ upg ->
if (!this.canWrite()) {
logger.warning("Device info file $this is not writable, failed to upgrade legacy device info.")
return@upg
}
try {
this.writeText(DeviceInfoManager.serialize(upg, json))
} catch (ex: SecurityException) {
logger.warning("Device info file $this is not writable, failed to upgrade legacy device info.", ex)
}
}
}
/** /**
* 生成随机 [DeviceInfo] * 生成随机 [DeviceInfo]
@ -153,7 +208,7 @@ public constructor(
* @since 2.0 * @since 2.0
*/ */
@JvmStatic @JvmStatic
public fun random(): DeviceInfo public fun random(): DeviceInfo = random(Random.Default)
/** /**
* 使用特定随机数生成器生成 [DeviceInfo] * 使用特定随机数生成器生成 [DeviceInfo]
@ -162,11 +217,9 @@ public constructor(
* @since 2.9 * @since 2.9
*/ */
@JvmStatic @JvmStatic
public fun random(random: Random): DeviceInfo public fun random(random: Random): DeviceInfo {
return DeviceInfoCommonImpl.randomDeviceInfo(random)
@Deprecated(DeviceInfoConstructorDeprecationMessage, level = DeprecationLevel.WARNING) }
@DeprecatedSinceMirai(warningSince = "2.15") // planned internal
public fun serializer(): KSerializer<DeviceInfo>
/** /**
* 将此 [DeviceInfo] 序列化为字符串. 序列化的字符串可以在以后通过 [DeviceInfo.deserializeFromString] 反序列化为 [DeviceInfo]. * 将此 [DeviceInfo] 序列化为字符串. 序列化的字符串可以在以后通过 [DeviceInfo.deserializeFromString] 反序列化为 [DeviceInfo].
@ -176,7 +229,7 @@ public constructor(
* @since 2.15 * @since 2.15
*/ */
@JvmStatic @JvmStatic
public fun serializeToString(deviceInfo: DeviceInfo): String public fun serializeToString(deviceInfo: DeviceInfo): String = DeviceInfoManager.serialize(deviceInfo)
/** /**
* 将通过 [serializeToString] 序列化得到的字符串反序列化为 [DeviceInfo]. * 将通过 [serializeToString] 序列化得到的字符串反序列化为 [DeviceInfo].
@ -184,19 +237,28 @@ public constructor(
* @since 2.15 * @since 2.15
*/ */
@JvmStatic @JvmStatic
public fun deserializeFromString(string: String): DeviceInfo public fun deserializeFromString(string: String): DeviceInfo = DeviceInfoManager.deserialize(string)
} }
/** /**
* @since 2.9 * @since 2.9
*/ */
@Suppress("DuplicatedCode") @Suppress("DuplicatedCode")
override fun equals(other: Any?): Boolean override fun equals(other: Any?): Boolean {
return DeviceInfoCommonImpl.equalsImpl(this, other)
}
/** /**
* @since 2.9 * @since 2.9
*/ */
override fun hashCode(): Int override fun hashCode(): Int {
return DeviceInfoCommonImpl.hashCodeImpl(this)
}
@Suppress("ClassName")
@Deprecated("For binary compatibility", level = DeprecationLevel.HIDDEN)
public object `$serializer` : KSerializer<DeviceInfo> by DeviceInfoV1LegacySerializer
} }
/** /**
@ -225,9 +287,9 @@ private class DevInfo @OptIn(ExperimentalSerializationApi::class) constructor(
/** /**
* 不要使用这个 API, API 在未来可能会被删除 * 不要使用这个 API, API 在未来可能会被删除
*/ */
@OptIn(ExperimentalSerializationApi::class)
public fun DeviceInfo.generateDeviceInfoData(): ByteArray { // ?? why is this public? public fun DeviceInfo.generateDeviceInfoData(): ByteArray { // ?? why is this public?
@OptIn(ExperimentalSerializationApi::class)
return ProtoBuf.encodeToByteArray( return ProtoBuf.encodeToByteArray(
DevInfo.serializer(), DevInfo( DevInfo.serializer(), DevInfo(
bootloader, bootloader,
@ -343,9 +405,7 @@ internal object DeviceInfoCommonImpl {
if (imei != other.imei) return false if (imei != other.imei) return false
if (!apn.contentEquals(other.apn)) return false if (!apn.contentEquals(other.apn)) return false
if (!guid.contentEquals(other.guid)) return false if (!guid.contentEquals(other.guid)) return false
if (!androidId.contentEquals(other.androidId)) return false return androidId.contentEquals(other.androidId)
return true
} }
@OptIn(MiraiInternalApi::class) @OptIn(MiraiInternalApi::class)

View File

@ -1,221 +0,0 @@
/*
* Copyright 2019-2023 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
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.json.Json
import java.io.File
import kotlin.random.Random
/**
* 表示设备信息
* @see DeviceInfoBuilder
*/
@Serializable(DeviceInfoV1LegacySerializer::class)
public actual class DeviceInfo
@Deprecated(DeviceInfoConstructorDeprecationMessage, level = DeprecationLevel.WARNING)
@DeprecatedSinceMirai(warningSince = "2.15") // planned internal
public actual constructor(
public actual val display: ByteArray,
public actual val product: ByteArray,
public actual val device: ByteArray,
public actual val board: ByteArray,
public actual val brand: ByteArray,
public actual val model: ByteArray,
public actual val bootloader: ByteArray,
public actual val fingerprint: ByteArray,
public actual val bootId: ByteArray,
public actual val procVersion: ByteArray,
public actual val baseBand: ByteArray,
public actual val version: Version,
public actual val simInfo: ByteArray,
public actual val osType: ByteArray,
public actual val macAddress: ByteArray,
public actual val wifiBSSID: ByteArray,
public actual val wifiSSID: ByteArray,
public actual val imsiMd5: ByteArray,
public actual val imei: String,
public actual val apn: ByteArray,
public actual val androidId: ByteArray,
) {
@Deprecated(
DeviceInfoConstructorDeprecationMessage,
replaceWith = ReplaceWith(
"net.mamoe.mirai.utils.DeviceInfo(display, product, device, board, brand, model, " +
"bootloader, fingerprint, bootId, procVersion, baseBand, version, simInfo, osType, " +
"macAddress, wifiBSSID, wifiSSID, imsiMd5, imei, apn, androidId)"
),
level = DeprecationLevel.WARNING
)
@DeprecatedSinceMirai(warningSince = "2.15")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public constructor(
display: ByteArray,
product: ByteArray,
device: ByteArray,
board: ByteArray,
brand: ByteArray,
model: ByteArray,
bootloader: ByteArray,
fingerprint: ByteArray,
bootId: ByteArray,
procVersion: ByteArray,
baseBand: ByteArray,
version: Version,
simInfo: ByteArray,
osType: ByteArray,
macAddress: ByteArray,
wifiBSSID: ByteArray,
wifiSSID: ByteArray,
imsiMd5: ByteArray,
imei: String,
apn: ByteArray
) : this(
display, product, device, board, brand, model, bootloader,
fingerprint, bootId, procVersion, baseBand, version, simInfo,
osType, macAddress, wifiBSSID, wifiSSID, imsiMd5, imei, apn,
androidId = display
)
public actual val ipAddress: ByteArray get() = byteArrayOf(192.toByte(), 168.toByte(), 1, 123)
init {
require(imsiMd5.size == 16) { "Bad `imsiMd5.size`. Required 16, given ${imsiMd5.size}." }
}
@Transient
@MiraiInternalApi
public actual val guid: ByteArray = generateGuid(androidId, macAddress)
@Serializable
public actual class Version actual constructor(
public actual val incremental: ByteArray,
public actual val release: ByteArray,
public actual val codename: ByteArray,
public actual val sdk: Int
) {
/**
* @since 2.9
*/
actual override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Version) return false
if (!incremental.contentEquals(other.incremental)) return false
if (!release.contentEquals(other.release)) return false
if (!codename.contentEquals(other.codename)) return false
if (sdk != other.sdk) return false
return true
}
/**
* @since 2.9
*/
actual override fun hashCode(): Int {
var result = incremental.contentHashCode()
result = 31 * result + release.contentHashCode()
result = 31 * result + codename.contentHashCode()
result = 31 * result + sdk
return result
}
}
public actual companion object {
internal actual val logger = MiraiLogger.Factory.create(DeviceInfo::class, "DeviceInfo")
/**
* 加载一个设备信息. 若文件不存在或为空则随机并创建一个设备信息保存.
*/
@JvmOverloads
@JvmStatic
@JvmName("from")
public fun File.loadAsDeviceInfo(
json: Json = DeviceInfoManager.format
): DeviceInfo {
if (!this.exists() || this.length() == 0L) {
return random().also {
this.writeText(DeviceInfoManager.serialize(it, json))
}
}
return DeviceInfoManager.deserialize(this.readText(), json) upg@{ upg ->
if (!this.canWrite()) {
logger.warning("Device info file $this is not writable, failed to upgrade legacy device info.")
return@upg
}
try {
this.writeText(DeviceInfoManager.serialize(upg, json))
} catch (ex: SecurityException) {
logger.warning("Device info file $this is not writable, failed to upgrade legacy device info.", ex)
}
}
}
/**
* 生成随机 [DeviceInfo]
*
* @see DeviceInfoBuilder
* @since 2.0
*/
@JvmStatic
public actual fun random(): DeviceInfo = random(Random.Default)
/**
* 使用特定随机数生成器生成 [DeviceInfo]
*
* @see DeviceInfoBuilder
* @since 2.9
*/
@JvmStatic
public actual fun random(random: Random): DeviceInfo {
return DeviceInfoCommonImpl.randomDeviceInfo(random)
}
/**
* 将此 [DeviceInfo] 序列化为字符串. 序列化的字符串可以在以后通过 [DeviceInfo.deserializeFromString] 反序列化为 [DeviceInfo].
*
* 序列化的字符串有兼容性保证, 在旧版 mirai 序列化的字符串, 可以在新版 mirai 使用. 但新版 mirai 序列化的字符串不一定能在旧版使用.
*
* @since 2.15
*/
@JvmStatic
public actual fun serializeToString(deviceInfo: DeviceInfo): String = DeviceInfoManager.serialize(deviceInfo)
/**
* 将通过 [serializeToString] 序列化得到的字符串反序列化为 [DeviceInfo].
* 此函数兼容旧版 mirai 序列化的字符串.
* @since 2.15
*/
@JvmStatic
public actual fun deserializeFromString(string: String): DeviceInfo = DeviceInfoManager.deserialize(string)
}
/**
* @since 2.9
*/
@Suppress("DuplicatedCode")
actual override fun equals(other: Any?): Boolean {
return DeviceInfoCommonImpl.equalsImpl(this, other)
}
/**
* @since 2.9
*/
actual override fun hashCode(): Int {
return DeviceInfoCommonImpl.hashCodeImpl(this)
}
@Suppress("ClassName")
@Deprecated("For binary compatibility", level = DeprecationLevel.HIDDEN)
public object `$serializer` : KSerializer<DeviceInfo> by DeviceInfoV1LegacySerializer
}