mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-17 09:39:49 +08:00
Fix multiplatform BotConfiguration
This commit is contained in:
parent
f8cbdf1c9b
commit
8eda95659c
@ -90,13 +90,13 @@ actual open class BotConfiguration actual constructor() {
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
@BotConfigurationDsl
|
||||
inline operator fun FileBasedDeviceInfo.unaryPlus() {
|
||||
deviceInfo = { File(filepath).loadAsDeviceInfo() }
|
||||
deviceInfo = { File(filepath).loadAsDeviceInfo(it) }
|
||||
}
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
@BotConfigurationDsl
|
||||
inline operator fun FileBasedDeviceInfo.ByDeviceDotJson.unaryPlus() {
|
||||
deviceInfo = { File("device.json").loadAsDeviceInfo() }
|
||||
deviceInfo = { File("device.json").loadAsDeviceInfo(it) }
|
||||
}
|
||||
|
||||
actual operator fun _NoNetworkLog.unaryPlus() {
|
||||
|
@ -16,8 +16,25 @@ import android.telephony.TelephonyManager
|
||||
import kotlinx.io.core.toByteArray
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
import kotlinx.serialization.UnstableDefault
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* 加载一个设备信息. 若文件不存在或为空则随机并创建一个设备信息保存.
|
||||
*/
|
||||
@UseExperimental(UnstableDefault::class)
|
||||
fun File.loadAsDeviceInfo(context: Context): DeviceInfo {
|
||||
if (!this.exists() || this.length() == 0L) {
|
||||
return SystemDeviceInfo(context).also {
|
||||
this.writeText(Json.plain.stringify(SystemDeviceInfo.serializer(), it))
|
||||
}
|
||||
}
|
||||
return Json.nonstrict.parse(DeviceInfoData.serializer(), this.readText()).also {
|
||||
it.context = context
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 部分引用指向 [Build].
|
||||
* 部分需要权限, 若无权限则会使用默认值.
|
||||
@ -99,7 +116,7 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
|
||||
override val apn: ByteArray get() = "wifi".toByteArray()
|
||||
|
||||
@Serializable
|
||||
object Version : DeviceInfo.Version {
|
||||
actual object Version : DeviceInfo.Version {
|
||||
override val incremental: ByteArray get() = Build.VERSION.INCREMENTAL.toByteArray()
|
||||
override val release: ByteArray get() = Build.VERSION.RELEASE.toByteArray()
|
||||
override val codename: ByteArray get() = Build.VERSION.CODENAME.toByteArray()
|
||||
|
@ -94,6 +94,45 @@ abstract class DeviceInfo {
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
class DeviceInfoData(
|
||||
override val display: ByteArray,
|
||||
override val product: ByteArray,
|
||||
override val device: ByteArray,
|
||||
override val board: ByteArray,
|
||||
override val brand: ByteArray,
|
||||
override val model: ByteArray,
|
||||
override val bootloader: ByteArray,
|
||||
override val fingerprint: ByteArray,
|
||||
override val bootId: ByteArray,
|
||||
override val procVersion: ByteArray,
|
||||
override val baseBand: ByteArray,
|
||||
override val version: VersionData,
|
||||
override val simInfo: ByteArray,
|
||||
override val osType: ByteArray,
|
||||
override val macAddress: ByteArray,
|
||||
override val wifiBSSID: ByteArray?,
|
||||
override val wifiSSID: ByteArray?,
|
||||
override val imsiMd5: ByteArray,
|
||||
override val imei: String,
|
||||
override val apn: ByteArray
|
||||
) : DeviceInfo() {
|
||||
@Transient
|
||||
override lateinit var context: Context
|
||||
|
||||
@UseExperimental(ExperimentalUnsignedTypes::class)
|
||||
override val ipAddress: ByteArray
|
||||
get() = localIpAddress().split(".").map { it.toUByte().toByte() }.takeIf { it.size == 4 }?.toByteArray() ?: byteArrayOf()
|
||||
override val androidId: ByteArray get() = display
|
||||
|
||||
@Serializable
|
||||
class VersionData(
|
||||
override val incremental: ByteArray = SystemDeviceInfo.Version.incremental,
|
||||
override val release: ByteArray = SystemDeviceInfo.Version.release,
|
||||
override val codename: ByteArray = SystemDeviceInfo.Version.codename,
|
||||
override val sdk: Int = SystemDeviceInfo.Version.sdk
|
||||
) : Version
|
||||
}
|
||||
/**
|
||||
* Defaults "%4;7t>;28<fc.5*6".toByteArray()
|
||||
*/
|
||||
|
@ -18,4 +18,6 @@ package net.mamoe.mirai.utils
|
||||
expect open class SystemDeviceInfo : DeviceInfo {
|
||||
constructor()
|
||||
constructor(context: Context)
|
||||
|
||||
object Version : DeviceInfo.Version
|
||||
}
|
@ -216,13 +216,13 @@ actual open class BotConfiguration actual constructor() {
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
@BotConfigurationDsl
|
||||
inline operator fun FileBasedDeviceInfo.unaryPlus() {
|
||||
deviceInfo = { File(filepath).loadAsDeviceInfo() }
|
||||
deviceInfo = { File(filepath).loadAsDeviceInfo(it) }
|
||||
}
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
@BotConfigurationDsl
|
||||
inline operator fun FileBasedDeviceInfo.ByDeviceDotJson.unaryPlus() {
|
||||
deviceInfo = { File("device.json").loadAsDeviceInfo() }
|
||||
deviceInfo = { File("device.json").loadAsDeviceInfo(it) }
|
||||
}
|
||||
|
||||
actual operator fun _NoNetworkLog.unaryPlus() {
|
||||
|
@ -68,50 +68,10 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
|
||||
override val apn: ByteArray = "wifi".toByteArray()
|
||||
|
||||
@Serializable
|
||||
object Version : DeviceInfo.Version {
|
||||
actual object Version : DeviceInfo.Version {
|
||||
override val incremental: ByteArray = "5891938".toByteArray()
|
||||
override val release: ByteArray = "10".toByteArray()
|
||||
override val codename: ByteArray = "REL".toByteArray()
|
||||
override val sdk: Int = 29
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
class DeviceInfoData(
|
||||
override val display: ByteArray,
|
||||
override val product: ByteArray,
|
||||
override val device: ByteArray,
|
||||
override val board: ByteArray,
|
||||
override val brand: ByteArray,
|
||||
override val model: ByteArray,
|
||||
override val bootloader: ByteArray,
|
||||
override val fingerprint: ByteArray,
|
||||
override val bootId: ByteArray,
|
||||
override val procVersion: ByteArray,
|
||||
override val baseBand: ByteArray,
|
||||
override val version: VersionData,
|
||||
override val simInfo: ByteArray,
|
||||
override val osType: ByteArray,
|
||||
override val macAddress: ByteArray,
|
||||
override val wifiBSSID: ByteArray?,
|
||||
override val wifiSSID: ByteArray?,
|
||||
override val imsiMd5: ByteArray,
|
||||
override val imei: String,
|
||||
override val apn: ByteArray
|
||||
) : DeviceInfo() {
|
||||
@Transient
|
||||
override lateinit var context: Context
|
||||
|
||||
@UseExperimental(ExperimentalUnsignedTypes::class)
|
||||
override val ipAddress: ByteArray
|
||||
get() = localIpAddress().split(".").map { it.toUByte().toByte() }.takeIf { it.size == 4 }?.toByteArray() ?: byteArrayOf()
|
||||
override val androidId: ByteArray get() = display
|
||||
|
||||
@Serializable
|
||||
class VersionData(
|
||||
override val incremental: ByteArray = SystemDeviceInfo.Version.incremental,
|
||||
override val release: ByteArray = SystemDeviceInfo.Version.release,
|
||||
override val codename: ByteArray = SystemDeviceInfo.Version.codename,
|
||||
override val sdk: Int = SystemDeviceInfo.Version.sdk
|
||||
) : Version
|
||||
}
|
Loading…
Reference in New Issue
Block a user