Skip to content

Commit

Permalink
uupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Colter23 committed Jun 27, 2022
1 parent f47c915 commit 8792d88
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 65 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ v3数据文件名 `BiliData.yml`
| `pushInterval` | 单位毫秒 | QQ中连续发送消息的间隔 |

#### ImageConfig
| 配置项 | 取值 | 说明 |
|------------------|-----------------------------------------------------------|--------------------------------------------|
| `quality` | `800w` / `1000w` / `1200w` / `1500w` (图片宽度) | 图片质量(分辨率) |
| `theme` | `v3` (新版绘图主题) / `v3RainbowOutline` (彩虹边框) / `v2` (旧版绘图主题) | 绘图主题 (目前两套其实都差不多,鸡肋功能) |
| `font` | 字体名 / 字体文件名(不用加后缀) | 绘图字体 目前仅支持单字体 字体放到插件数据路径下 `font` 文件夹中 |
| `defaultColor` | HEX颜色值 (#d3edfa;#fde8ed) | 默认绘图主题色 支持多个值自定义渐变 中间用分号`;`号分隔 单个值会自动生成渐变色 |
| `cardOrnament` | `FanCard` (粉丝卡片) / `QrCode` (动态链接二维码) / `None` (无) | 卡片装饰 |
| `colorGenerator` | [ColorGenerator](#ColorGenerator) | 渐变色生成器配置 (用于图片背景) 仅当主题色为单个值时会自动生成 |
| `badgeEnable` | `true` / `false` | 卡片顶部的标签 |
| 配置项 | 取值 | 说明 |
|------------------|-----------------------------------------------------------|----------------------------------------------------|
| `quality` | `800w` / `1000w` / `1200w` / `1500w` (图片宽度) | 图片质量(分辨率) |
| `theme` | `v3` (新版绘图主题) / `v3RainbowOutline` (彩虹边框) / `v2` (旧版绘图主题) | 绘图主题 (目前两套其实都差不多,鸡肋功能) |
| `font` | 字体名 / 字体文件名(不用加后缀) | 绘图字体 目前仅支持单字体 字体放到插件数据路径下 `font` 文件夹中 -> [字体](#字体) |
| `defaultColor` | HEX颜色值 (#d3edfa;#fde8ed) | 默认绘图主题色 支持多个值自定义渐变 中间用分号`;`号分隔 单个值会自动生成渐变色 |
| `cardOrnament` | `FanCard` (粉丝卡片) / `QrCode` (动态链接二维码) / `None` (无) | 卡片装饰 |
| `colorGenerator` | [ColorGenerator](#ColorGenerator) | 渐变色生成器配置 (用于图片背景) 仅当主题色为单个值时会自动生成 |
| `badgeEnable` | `true` / `false` | 卡片顶部的标签 |

##### ColorGenerator
| 配置项 | 取值 | 说明 |
Expand Down Expand Up @@ -810,6 +810,13 @@ v2:
通过对群设置权限来控制是否开启直播@全体
`/perm add g123456789 top.colter.bilibili-dynamic-mirai-plugin:live.atall`

### 字体

[HarmonyOS Sans](https://developer.harmonyos.com/cn/docs/design/des-resources/general-0000001157315901)

选择下载 52.2MB 的字体压缩包文件, 请使用压缩包内 `HarmonyOS_Sans_SC` 目录下的字体, 此目录下的字体为简体中文
里面不同文件代表不同的粗细, 建议使用 `Medium`

### 手动获取 Cookie
<details>
<summary>点击展开</summary>
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 = "top.colter"
version = "3.0.0-M3"
version = "3.0.0-M4"

repositories {
mavenLocal()
Expand Down
Binary file removed font/HarmonyOS_Sans_SC_Medium.ttf
Binary file not shown.
Binary file removed font/思源黑体.ttf
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object BiliBiliDynamic : KotlinPlugin(
JvmPluginDescription(
id = "top.colter.bilibili-dynamic-mirai-plugin",
name = "BiliBili Dynamic",
version = "3.0.0-M3",
version = "3.0.0-M4",
) {
author("Colter")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import top.colter.mirai.plugin.bilibili.BiliBiliDynamic
import top.colter.mirai.plugin.bilibili.FilterMode
import top.colter.mirai.plugin.bilibili.FilterType
import top.colter.mirai.plugin.bilibili.api.getDynamicDetail
import top.colter.mirai.plugin.bilibili.api.getLive
import top.colter.mirai.plugin.bilibili.data.DynamicDetail
import top.colter.mirai.plugin.bilibili.data.LiveDetail
import top.colter.mirai.plugin.bilibili.tasker.BiliDataTasker
import top.colter.mirai.plugin.bilibili.utils.biliClient
import top.colter.mirai.plugin.bilibili.utils.delegate
Expand Down Expand Up @@ -115,6 +117,14 @@ object DynamicCommand : CompositeCommand(
detail?.let { d -> BiliBiliDynamic.dynamicChannel.send(DynamicDetail(d, subject.delegate)) }
}

@SubCommand("live", "直播")
suspend fun CommandSenderOnMessage<*>.live() {
val subject = Contact()
val detail = biliClient.getLive(1, 1)
if (detail != null) subject.sendMessage("请稍等") else subject.sendMessage("当前没有人在直播")
detail?.let { d -> BiliBiliDynamic.liveChannel.send(LiveDetail(d.rooms.first(), subject.delegate)) }
}

}

fun CommandSender.Contact(): Contact = subject ?: throw CommandArgumentParserException("无法从当前环境获取联系人")
12 changes: 11 additions & 1 deletion src/main/kotlin/top/colter/mirai/plugin/bilibili/data/Dynamic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,17 @@ data class ModuleFold(
@SerialName("statement")
val statement: String,
@SerialName("users")
val users: List<String>,
val users: List<BaseUser>,
)

@Serializable
data class BaseUser(
@SerialName("mid")
val mid: Long,
@SerialName("name")
val name: String,
@SerialName("face")
val face: String,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,34 @@ val cardContentRect: Rect by lazy {
val mainTypeface: Typeface by lazy {
val mainFont = imageConfig.font.split(";").first().split(".").first()
try {
matchFamily(mainFont).matchStyle(FontStyle.NORMAL)!!
if (mainFont.isBlank()){
logger.warning("配置文件未配置字体, 尝试加载 font 目录下的字体")
val f = FontUtils.defaultFont
if (f == null){
throw Exception()
}else {
logger.info("成功加载 ${f.familyName} 字体")
return@lazy f
}
}else {
matchFamily(mainFont).matchStyle(FontStyle.NORMAL)!!
}
} catch (e: Exception) {
logger.error("加载主字体 $mainFont 失败")
matchFamily("Source Han Sans").matchStyle(FontStyle.NORMAL)!!
logger.warning("加载主字体 $mainFont 失败, 尝试加载默认字体")
loadSysDefaultFont()
}
}

fun loadSysDefaultFont(): Typeface {
val defaultList = listOf("HarmonyOS Sans SC", "Source Han Sans", "SimHei", "sans-serif")
defaultList.forEach {
try {
val f = matchFamily(it).matchStyle(FontStyle.NORMAL)!!
logger.info("加载默认字体 $it 成功")
return f
} catch (_: Exception) { }
}
throw Exception("无法加载默认字体, 请自行配置字体或准备字体文件")
}

val font: Font by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,8 @@ suspend fun ModuleDynamic.Major.Article.drawGeneral(): Image {
clipRRect(coverRRect, true)
covers.forEach {
val img = getOrDownloadImage(it, CacheType.IMAGES)
val src = Rect.makeXYWH(0f, 0f, img.width.toFloat(), img.height.toFloat())
val tar = Rect.makeXYWH(imgX, articleCardRect.top, imgW, articleCoverHeight)
drawImageRect(img, src, tar, Paint())
val tar = RRect.makeXYWH(imgX, articleCardRect.top, imgW, articleCoverHeight, 0f)
drawImageClip(img, tar, Paint())
imgX += articleCardRect.width / 3 + 2
}
restore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import top.colter.mirai.plugin.bilibili.utils.FontUtils
import top.colter.mirai.plugin.bilibili.utils.formatTime
import top.colter.mirai.plugin.bilibili.utils.getOrDownloadImage
import top.colter.mirai.plugin.bilibili.utils.translate.trans
import kotlin.math.abs
import kotlin.math.ceil


Expand Down Expand Up @@ -455,14 +456,14 @@ suspend fun ModuleAuthor.drawGeneral(time: String, link: String, themeColor: Int
y += quality.subTitleFontSize + space * 0.5f
drawTextLine(textLineTime, x, y, Paint().apply { color = theme.subTitleColor })

drawOrnament(BiliConfig.imageConfig.cardOrnament, decorate, link, themeColor, null)
drawOrnament(decorate, link, themeColor)
}
}.makeImageSnapshot()
}

suspend fun Canvas.drawOrnament(cardOrnament: String, decorate: ModuleAuthor.Decorate?, link: String?, qrCodeColor: Int?, label: String?) {
suspend fun Canvas.drawOrnament(decorate: ModuleAuthor.Decorate?, link: String?, qrCodeColor: Int?) {

when (cardOrnament) {
when (BiliConfig.imageConfig.cardOrnament) {
"FanCard" -> {
if (decorate != null) {
val fanImg = getOrDownloadImage(decorate.cardUrl, CacheType.USER)
Expand All @@ -475,10 +476,10 @@ suspend fun Canvas.drawOrnament(cardOrnament: String, decorate: ModuleAuthor.Dec

val cardWidth = fanImg.width * cardHeight / fanImg.height

val y = ((quality.faceSize - cardHeight) / 2) + quality.cardPadding
val y = ((quality.faceSize - cardHeight + quality.contentSpace) / 2)
val tarFRect = Rect.makeXYWH(
cardContentRect.right - cardWidth - 20f,
y,
cardContentRect.right - cardWidth - abs(y),
y + quality.cardPadding,
cardWidth,
cardHeight
)
Expand Down Expand Up @@ -506,12 +507,12 @@ suspend fun Canvas.drawOrnament(cardOrnament: String, decorate: ModuleAuthor.Dec

"QrCode" -> {
val qrCodeImg = qrCode(link!!, quality.ornamentHeight.toInt(), qrCodeColor!!)
val y = ((quality.faceSize - quality.ornamentHeight) / 2) + quality.cardPadding
val y = ((quality.faceSize - qrCodeImg.height + quality.contentSpace) / 2)
val tarFRect = Rect.makeXYWH(
cardContentRect.right - quality.ornamentHeight - 20f,
y,
quality.ornamentHeight,
quality.ornamentHeight
cardContentRect.right - qrCodeImg.width - abs(y),
y + quality.cardPadding,
qrCodeImg.width.toFloat(),
qrCodeImg.height.toFloat()
)
val srcFRect = Rect(0f, 0f, qrCodeImg.width.toFloat(), qrCodeImg.height.toFloat())
drawImageRect(
Expand All @@ -523,21 +524,5 @@ suspend fun Canvas.drawOrnament(cardOrnament: String, decorate: ModuleAuthor.Dec
true
)
}

"Label" -> {
val labelTextLine = TextLine.make(label, font.makeWithSize(quality.subTitleFontSize))
val y = ((quality.faceSize - quality.ornamentHeight) / 2) + quality.cardPadding
drawLabelCard(
labelTextLine,
cardContentRect.right - labelTextLine.width - quality.badgePadding * 4,
y,
Paint().apply {
color = theme.subLeftBadge.fontColor
},
Paint().apply {
color = theme.subLeftBadge.bgColor
}
)
}
}
}
47 changes: 44 additions & 3 deletions src/main/kotlin/top/colter/mirai/plugin/bilibili/draw/LiveDraw.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import org.jetbrains.skia.svg.SVGDOM
import top.colter.mirai.plugin.bilibili.BiliConfig
import top.colter.mirai.plugin.bilibili.BiliData
import top.colter.mirai.plugin.bilibili.data.LiveInfo
import top.colter.mirai.plugin.bilibili.tasker.DynamicMessageTasker
import top.colter.mirai.plugin.bilibili.utils.*
import kotlin.math.abs


suspend fun LiveInfo.makeDrawLive(colors: List<Int>): String {
Expand Down Expand Up @@ -92,7 +92,7 @@ suspend fun LiveInfo.drawLive(): Image {

suspend fun LiveInfo.drawAvatar(): Image {
return Surface.makeRasterN32Premul(
quality.imageWidth - quality.cardMargin * 2,
cardRect.width.toInt(),
(quality.faceSize + quality.cardPadding * 2f).toInt()
).apply surface@{
canvas.apply {
Expand All @@ -113,7 +113,48 @@ suspend fun LiveInfo.drawAvatar(): Image {

val color = BiliData.dynamic[uid]?.color ?: BiliConfig.imageConfig.defaultColor
val colors = color.split(";", "").map { Color.makeRGB(it.trim()) }.first()
drawOrnament(if (BiliConfig.imageConfig.cardOrnament=="QrCode") "QrCode" else "Label", null, "https://live.bilibili.com/$roomId", colors, area)
drawLiveOrnament("https://live.bilibili.com/$roomId", colors, area)
}
}.makeImageSnapshot()
}

fun Canvas.drawLiveOrnament(link: String?, qrCodeColor: Int?, label: String?) {
when (BiliConfig.imageConfig.cardOrnament) {
"QrCode" -> {
val qrCodeImg = qrCode(link!!, quality.ornamentHeight.toInt(), qrCodeColor!!)
val y = ((quality.faceSize - qrCodeImg.height + quality.contentSpace) / 2)
val tarFRect = Rect.makeXYWH(
cardRect.width - qrCodeImg.width - abs(y),
y + quality.cardPadding,
qrCodeImg.width.toFloat(),
qrCodeImg.height.toFloat()
)

val srcFRect = Rect.makeXYWH(0f, 0f, qrCodeImg.width.toFloat(), qrCodeImg.height.toFloat())
drawImageRect(
qrCodeImg,
srcFRect,
tarFRect,
FilterMipmap(FilterMode.LINEAR, MipmapMode.NEAREST),
Paint(),
true
)
}
"None" -> {}
else -> {
val labelTextLine = TextLine.make(label, font.makeWithSize(quality.subTitleFontSize))
val y = ((quality.faceSize - quality.subTitleFontSize - quality.badgePadding * 2 + quality.contentSpace) / 2)
drawLabelCard(
labelTextLine ,
cardContentRect.right - labelTextLine.width - quality.badgePadding * 4 - abs(y),
y + quality.cardPadding,
Paint().apply {
color = theme.subLeftBadge.fontColor
},
Paint().apply {
color = theme.subLeftBadge.bgColor
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import top.colter.mirai.plugin.bilibili.BiliConfig.accountConfig
import top.colter.mirai.plugin.bilibili.api.*
import top.colter.mirai.plugin.bilibili.client.BiliClient
import top.colter.mirai.plugin.bilibili.data.DynamicMessage
import top.colter.mirai.plugin.bilibili.data.DynamicType
import top.colter.mirai.plugin.bilibili.data.LiveMessage
import top.colter.mirai.plugin.bilibili.data.LoginData
import top.colter.mirai.plugin.bilibili.draw.loginQrCode
import top.colter.mirai.plugin.bilibili.tasker.DynamicMessageTasker.buildMessage
import top.colter.mirai.plugin.bilibili.tasker.LiveMessageTasker.buildMessage
import top.colter.mirai.plugin.bilibili.tasker.SendTasker.buildMessage
import top.colter.mirai.plugin.bilibili.utils.biliClient
import top.colter.mirai.plugin.bilibili.utils.decode
import top.colter.mirai.plugin.bilibili.utils.delegate
import top.colter.mirai.plugin.bilibili.utils.findContact
import java.net.URI
import java.time.Instant

internal val logger by BiliBiliDynamic::logger

Expand Down Expand Up @@ -333,17 +335,11 @@ object BiliDataTasker {
}

// https://t.bilibili.com/385190177693666264
val dynamic = DynamicMessage(
"100000000000114514",
114514,
"哼啊啊啊",
DynamicType.DYNAMIC_TYPE_WORD,
"2114年5月14日 11:45:14",
Instant.now().epochSecond.toInt(),
"测试内容测试内容测试内容",
null,
listOf(DynamicMessage.Link("", "https://t.bilibili.com/100000000000114514"))
)
val dynamic = if (type == "d"){
biliClient.getDynamicDetail("385190177693666264")?.buildMessage()!!
}else {
biliClient.getLive(1,1)?.rooms?.first()?.buildMessage()!!
}

subject.sendMessage(buildForwardMessage(subject) {
var pt = 0
Expand All @@ -352,8 +348,17 @@ object BiliDataTasker {
for (t in template) {
subject.bot named dynamic.uname at dynamic.timestamp + pt says t.key
subject.bot named dynamic.uname at dynamic.timestamp + pt says buildForwardMessage(subject) {
dynamic.buildMessage(t.value, subject).forEach {
subject.bot named dynamic.uname at dynamic.timestamp + pt says it
when (dynamic) {
is DynamicMessage -> {
dynamic.buildMessage(t.value, subject).forEach {
subject.bot named dynamic.uname at dynamic.timestamp + pt says it
}
}
is LiveMessage -> {
dynamic.buildMessage(t.value, subject).forEach {
subject.bot named dynamic.uname at dynamic.timestamp + pt says it
}
}
}
}
pt += 86400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object ListenerTasker : BiliTasker() {
//if (l != null) {
// val detail = biliClient.getLive(1, 1)
// if (detail != null) it.subject.sendMessage("绘图中,请稍等")
// detail?.let { d -> liveChannel.send(LiveDetail(d.rooms.first(), it.subject.delegate)) }
// detail?.let { d -> BiliBiliDynamic.liveChannel.send(LiveDetail(d.rooms.first(), it.subject.delegate)) }
//}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ object SendTasker : BiliTasker() {
val hasPerm = it.permitteeId.getPermittedPermissions().any { it.id == gwp }
if (hasPerm) {
val last = it1.last().plus("\n").plus(AtAll)
it.sendMessage(it1.dropLast(1).plus(last))
it.sendMessage(it1.dropLast(1).plusElement(last))
} else {
it.sendMessage(it1)
}
Expand Down
Loading

0 comments on commit 8792d88

Please sign in to comment.