Skip to content

Commit

Permalink
[core] Move DeviceInfo from jvmBase to common
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Aug 12, 2023
1 parent af89f4d commit 42763fd
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 300 deletions.
218 changes: 139 additions & 79 deletions mirai-core-api/src/commonMain/kotlin/utils/DeviceInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.json.Json
import kotlinx.serialization.protobuf.ProtoBuf
import kotlinx.serialization.protobuf.ProtoNumber
import kotlin.jvm.JvmStatic
import kotlin.jvm.JvmSynthetic
import java.io.File
import kotlin.random.Random

internal const val DeviceInfoConstructorDeprecationMessage =
Expand Down Expand Up @@ -56,95 +56,150 @@ internal const val DeviceInfoConstructorReplaceWith = "DeviceInfoBuilder.create(
* 表示设备信息
* @see DeviceInfoBuilder
*/
public expect class DeviceInfo
@Serializable(DeviceInfoV1LegacySerializer::class)
public class DeviceInfo
@Deprecated(
DeviceInfoConstructorDeprecationMessage, level = DeprecationLevel.WARNING,
DeviceInfoConstructorDeprecationMessage,
level = DeprecationLevel.WARNING,
replaceWith = ReplaceWith(
DeviceInfoConstructorReplaceWith,
"net.mamoe.mirai.utils.DeviceInfoBuilder"
)
)
@DeprecatedSinceMirai(warningSince = "2.15") // planned internal
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,
androidId: ByteArray,
public val display: ByteArray,
public val product: ByteArray,
public val device: ByteArray,
public val board: ByteArray,
public val brand: ByteArray,
public val model: ByteArray,
public val bootloader: ByteArray,
public val fingerprint: ByteArray,
public val bootId: ByteArray,
public val procVersion: ByteArray,
public val baseBand: ByteArray,
public val version: Version,
public val simInfo: ByteArray,
public val osType: ByteArray,
public val macAddress: ByteArray,
public val wifiBSSID: ByteArray,
public val wifiSSID: ByteArray,
public val imsiMd5: ByteArray,
public val imei: String,
public val apn: ByteArray,
public val androidId: ByteArray,
) {
public val display: ByteArray
public val product: ByteArray
public val device: ByteArray
public val board: ByteArray
public val brand: ByteArray
public val model: ByteArray
public val bootloader: ByteArray
public val fingerprint: ByteArray
public val bootId: ByteArray
public val procVersion: ByteArray
public val baseBand: ByteArray
public val version: Version
public val simInfo: ByteArray
public val osType: ByteArray
public val macAddress: ByteArray
public val wifiBSSID: ByteArray
public val wifiSSID: ByteArray
public val imsiMd5: ByteArray
public val imei: String
public val apn: ByteArray
public val androidId: ByteArray

public val ipAddress: 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 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 val guid: ByteArray
public val guid: ByteArray = generateGuid(androidId, macAddress)

// @Serializable: use DeviceInfoVersionSerializer in commonMain.
@Serializable
public class Version(
incremental: ByteArray = "5891938".toByteArray(),
release: ByteArray = "10".toByteArray(),
codename: ByteArray = "REL".toByteArray(),
sdk: Int = 29
public val incremental: ByteArray = "5891938".toByteArray(),
public val release: ByteArray = "10".toByteArray(),
public val codename: ByteArray = "REL".toByteArray(),
public val sdk: Int = 29
) {
public val incremental: ByteArray
public val release: ByteArray
public val codename: ByteArray
public val sdk: Int

/**
* @since 2.9
*/
override fun equals(other: Any?): Boolean
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
*/
override fun hashCode(): Int

internal companion object {
fun serializer(): KSerializer<Version>
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 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]
Expand All @@ -153,7 +208,7 @@ public constructor(
* @since 2.0
*/
@JvmStatic
public fun random(): DeviceInfo
public fun random(): DeviceInfo = random(Random.Default)

/**
* 使用特定随机数生成器生成 [DeviceInfo]
Expand All @@ -162,11 +217,9 @@ public constructor(
* @since 2.9
*/
@JvmStatic
public fun random(random: Random): DeviceInfo

@Deprecated(DeviceInfoConstructorDeprecationMessage, level = DeprecationLevel.WARNING)
@DeprecatedSinceMirai(warningSince = "2.15") // planned internal
public fun serializer(): KSerializer<DeviceInfo>
public fun random(random: Random): DeviceInfo {
return DeviceInfoCommonImpl.randomDeviceInfo(random)
}

/**
* 将此 [DeviceInfo] 序列化为字符串. 序列化的字符串可以在以后通过 [DeviceInfo.deserializeFromString] 反序列化为 [DeviceInfo].
Expand All @@ -176,27 +229,36 @@ public constructor(
* @since 2.15
*/
@JvmStatic
public fun serializeToString(deviceInfo: DeviceInfo): String
public fun serializeToString(deviceInfo: DeviceInfo): String = DeviceInfoManager.serialize(deviceInfo)

/**
* 将通过 [serializeToString] 序列化得到的字符串反序列化为 [DeviceInfo].
* 此函数兼容旧版 mirai 序列化的字符串.
* @since 2.15
*/
@JvmStatic
public fun deserializeFromString(string: String): DeviceInfo
public fun deserializeFromString(string: String): DeviceInfo = DeviceInfoManager.deserialize(string)
}

/**
* @since 2.9
*/
@Suppress("DuplicatedCode")
override fun equals(other: Any?): Boolean
override fun equals(other: Any?): Boolean {
return DeviceInfoCommonImpl.equalsImpl(this, other)
}


/**
* @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
}

/**
Expand Down Expand Up @@ -225,9 +287,9 @@ private class DevInfo @OptIn(ExperimentalSerializationApi::class) constructor(
/**
* 不要使用这个 API, 此 API 在未来可能会被删除
*/
@OptIn(ExperimentalSerializationApi::class)
public fun DeviceInfo.generateDeviceInfoData(): ByteArray { // ?? why is this public?

@OptIn(ExperimentalSerializationApi::class)
return ProtoBuf.encodeToByteArray(
DevInfo.serializer(), DevInfo(
bootloader,
Expand Down Expand Up @@ -343,9 +405,7 @@ internal object DeviceInfoCommonImpl {
if (imei != other.imei) return false
if (!apn.contentEquals(other.apn)) return false
if (!guid.contentEquals(other.guid)) return false
if (!androidId.contentEquals(other.androidId)) return false

return true
return androidId.contentEquals(other.androidId)
}

@OptIn(MiraiInternalApi::class)
Expand Down
Loading

0 comments on commit 42763fd

Please sign in to comment.