Skip to content

Commit

Permalink
build: 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Jan 28, 2022
1 parent 69d02c1 commit 61d0995
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 94 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[![maven-central](https://img.shields.io/maven-central/v/xyz.cssxsh.mirai/mirai-device-generator)](https://search.maven.org/artifact/xyz.cssxsh.mirai/mirai-device-generator)

作为插件运行时会检测 `mcl/bots/.../device.json`, `mcl/device.json` 是否存在,不存在则生成。
作为插件运行时会提供 `BotConfigurationAlterer` 服务,自动替换 `configuration.deviceInfo = generator::load`
作为 `mirai-core` 库引用时, 请自行调用 `configuration.deviceInfo = generator::load`

## board 编号

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "xyz.cssxsh.mirai"
version = "1.0.0-dev-3"
version = "1.0.0"

mavenCentralPublish {
useCentralS01()
Expand Down
51 changes: 0 additions & 51 deletions src/main/kotlin/net/mamoe/mirai/spi/DeviceInfoService.kt

This file was deleted.

10 changes: 3 additions & 7 deletions src/main/kotlin/xyz/cssxsh/mirai/MiraiDeviceGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package xyz.cssxsh.mirai

import kotlinx.serialization.*
import net.mamoe.mirai.*
import net.mamoe.mirai.spi.*
import net.mamoe.mirai.utils.*
import kotlin.random.*

@OptIn(MiraiExperimentalApi::class)
@Suppress("unused")
public class MiraiDeviceGenerator : DeviceInfoService {

override val priority: Int get() = super.priority - 1
public class MiraiDeviceGenerator {

internal var random: Random = Random.Default

Expand Down Expand Up @@ -49,7 +45,7 @@ public class MiraiDeviceGenerator : DeviceInfoService {
)

@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
public override fun load(bot: Bot): DeviceInfo {
public fun load(bot: Bot): DeviceInfo {
val file = bot.configuration.workingDir.resolve("device.json")
if (!file.exists() || file.length() == 0L) {
return generate().also {
Expand All @@ -59,7 +55,7 @@ public class MiraiDeviceGenerator : DeviceInfoService {
return DeviceInfoManager.deserialize(file.readText())
}

public override fun generate(): DeviceInfo {
public fun generate(): DeviceInfo {
val model = models.random(random)
val sdk = model.sdks.randomOrNull(random) ?: sdks.random(random)
return DeviceInfo(
Expand Down
43 changes: 9 additions & 34 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/MiraiDevicePlugin.kt
Original file line number Diff line number Diff line change
@@ -1,79 +1,54 @@
package xyz.cssxsh.mirai.plugin

import kotlinx.coroutines.*
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.*
import net.mamoe.mirai.console.extension.*
import net.mamoe.mirai.console.extensions.*
import net.mamoe.mirai.console.plugin.jvm.*
import net.mamoe.mirai.spi.*
import net.mamoe.mirai.utils.*
import xyz.cssxsh.mirai.*

public object MiraiDevicePlugin : KotlinPlugin(
JvmPluginDescription(
id = "xyz.cssxsh.mirai.mirai-device-generator",
name = "mirai-device-generator",
version = "1.0.0-dev-3",
version = "1.0.0",
) {
author("cssxsh")
}
) {
private val generator = MiraiDeviceGenerator()
private val json = Json { prettyPrint = true }

@OptIn(MiraiExperimentalApi::class)
override fun PluginComponentStorage.onLoad() {
DeviceInfoService.setService(generator)
contribute(BotConfigurationAlterer) {
BotConfigurationAlterer { _, configuration ->
configuration.deviceInfo = generator::load
configuration
}
}
}

@OptIn(MiraiExperimentalApi::class, ExperimentalSerializationApi::class)
override fun onEnable() {
val json = Json { prettyPrint = true }

with(dataFolder.resolve("models.json")) {
if (exists().not()) {
writeText(getResource("xyz/cssxsh/mirai/plugin/models.json") ?: throw NoSuchElementException("models.json"))
}
generator.models = json.decodeFromString(readText())
}

with(dataFolder.resolve("sdks.json")) {
if (exists().not()) {
writeText(getResource("xyz/cssxsh/mirai/plugin/sdks.json") ?: throw NoSuchElementException("sdks.json"))
}
generator.sdks = json.decodeFromString(readText())
}

with(dataFolder.resolve("mac.json")) {
if (exists().not()) {
writeText(getResource("xyz/cssxsh/mirai/plugin/mac.json") ?: throw NoSuchElementException("mac.json"))
}
generator.addr = json.decodeFromString(readText())
}

launch {
while (isActive) {
val device = MiraiConsole.rootDir.resolve("device.json")
if (device.exists().not()) {
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
device.writeText(DeviceInfoManager.serialize(generator.generate()))
}
delay(10_000)
}
}

launch {
while (isActive) {
Bot.instances.forEach { bot ->
val device = bot.configuration.workingDir.resolve("device.json")
if (device.exists().not()) {
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
device.writeText(DeviceInfoManager.serialize(generator.generate()))
}
}
delay(10_000)
}
}
}
}

0 comments on commit 61d0995

Please sign in to comment.