[Review] Move File.loadAsDeviceInfo to DeviceInfo companion

This commit is contained in:
Him188 2021-01-06 15:26:35 +08:00
parent a511bfc3b4
commit 3b7be90a84
2 changed files with 21 additions and 13 deletions

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 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -19,18 +19,6 @@ import net.mamoe.mirai.utils.internal.getRandomIntString
import net.mamoe.mirai.utils.internal.getRandomString
import java.io.File
/**
* 加载一个设备信息. 若文件不存在或为空则随机并创建一个设备信息保存.
*/
public fun File.loadAsDeviceInfo(json: Json): DeviceInfo {
if (!this.exists() || this.length() == 0L) {
return DeviceInfo.random().also {
this.writeText(json.encodeToString(DeviceInfo.serializer(), it))
}
}
return json.decodeFromString(DeviceInfo.serializer(), this.readText())
}
@Serializable
public class DeviceInfo(
public val display: ByteArray,
@ -66,6 +54,25 @@ public class DeviceInfo(
)
public companion object {
/**
* 加载一个设备信息. 若文件不存在或为空则随机并创建一个设备信息保存.
*/
@JvmOverloads
@JvmStatic
@JvmName("from")
public fun File.loadAsDeviceInfo(
json: Json = Json
): DeviceInfo {
if (!this.exists() || this.length() == 0L) {
return random().also {
this.writeText(json.encodeToString(serializer(), it))
}
}
return json.decodeFromString(serializer(), this.readText())
}
@JvmStatic
public fun random(): DeviceInfo {
return DeviceInfo(

View File

@ -20,6 +20,7 @@ import kotlinx.coroutines.withContext
import net.mamoe.mirai.Bot
import net.mamoe.mirai.network.LoginFailedException
import net.mamoe.mirai.network.NoStandardInputForCaptchaException
import net.mamoe.mirai.utils.DeviceInfo.Companion.loadAsDeviceInfo
import net.mamoe.mirai.utils.LoginSolver.Companion.Default
import net.mamoe.mirai.utils.StandardCharImageLoginSolver.Companion.createBlocking
import net.mamoe.mirai.utils.internal.SeleniumLoginSolver