Skip to content

Commit

Permalink
feat(merge danmaku): fix bug & process danmaku content
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Apr 8, 2024
1 parent 033fbc5 commit 3815a50
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import com.kuaishou.akdanmaku.ecs.component.filter.DuplicateMergedFilter
import com.muedsa.agetv.BuildConfig
import com.muedsa.agetv.model.LazyType
import com.muedsa.agetv.viewmodel.PlaybackViewModel
Expand Down Expand Up @@ -128,6 +129,7 @@ fun PlaybackScreen(
textSizeScale = danmakuSetting.danmakuSizeScale / 100f
alpha = danmakuSetting.danmakuAlpha / 100f
screenPart = danmakuSetting.danmakuScreenPart / 100f
dataFilter = listOf(DuplicateMergedFilter().apply { enable = true })
},
danmakuPlayerInit = {
if (!danmakuListLD.data.isNullOrEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ fun List<DanmakuItemData>.mergeDanmaku(
val fixedCache: MutableMap<String, SimilarDanmakuItemData> = mutableMapOf()
val result = mutableListOf<DanmakuItemData>()
this.sorted().forEach {
if (it.mode == DanmakuItemData.DANMAKU_MODE_ROLLING) {
mergedDanmaku(rollingCache, it, result, nearMs, maxMs, mergedSize)
} else if (it.mode == DanmakuItemData.DANMAKU_MODE_CENTER_TOP
|| it.mode == DanmakuItemData.DANMAKU_MODE_CENTER_BOTTOM
) {
mergedDanmaku(fixedCache, it, result, nearMs, maxMs, mergedSize)
when (it.mode) {
DanmakuItemData.DANMAKU_MODE_ROLLING ->
mergedDanmaku(rollingCache, it, result, nearMs, maxMs, mergedSize)

DanmakuItemData.DANMAKU_MODE_CENTER_TOP, DanmakuItemData.DANMAKU_MODE_CENTER_BOTTOM ->
mergedDanmaku(rollingCache, it, result, nearMs, maxMs, mergedSize)

else -> result.add(it)
}
}
rollingCache.forEach { (_, v) -> v.complete(result, mergedSize) }
Expand All @@ -148,15 +150,15 @@ private fun mergedDanmaku(
result.add(item)
return
}

val existed = cache[item.content]
val processedContent = processContent(item.content)
val existed = cache[processedContent]
if (existed == null) {
cache[item.content] = SimilarDanmakuItemData(item.content, item)
cache[processedContent] = SimilarDanmakuItemData(processedContent, item)
} else if (existed.near(nearMs, maxMs, item.position)) {
existed.inc(item, result)
} else {
existed.complete(result, mergedSize)
cache[item.content] = SimilarDanmakuItemData(item.content, item)
cache[processedContent] = SimilarDanmakuItemData(processedContent, item)
result.add(item.copyWith(mergedType = DanmakuItemData.MERGED_TYPE_ORIGINAL))
}
}
Expand All @@ -181,7 +183,7 @@ class SimilarDanmakuItemData(
DanmakuItemData(
danmakuId = Random.nextLong(),
position = first.position + 100,
content = "($number)$content",
content = "($number)${first.content}",
mode = first.mode,
textSize = mergedSize,
textColor = first.textColor,
Expand Down Expand Up @@ -224,3 +226,124 @@ fun DanmakuItemData.copyWith(
userId = userId ?: this.userId,
mergedType = mergedType ?: this.mergedType
)
private fun processContent(content: String): String {
if (content.isBlank()) {
return ""
}
return removeEndingChars(normalizeContent(content))
}

private val ENDING_CHARS = ".。,,/??!!…~~@^、+=-_♂♀".toCharArray()
private fun removeEndingChars(content: String): String {
if (content.isEmpty()) {
return content
}
if (ENDING_CHARS.contains(content.last())) {
return removeEndingChars(content.substring(0, content.length - 1).trimEnd())
}
return content
}

private val REPLACE_CHAR_MAP = mapOf(
' ' to ' ',
'' to '1',
'' to '2',
'' to '3',
'' to '4',
'' to '5',
'' to '6',
'' to '7',
'' to '8',
'' to '9',
'' to '0',
'!' to '',
'' to '@',
'' to '#',
'' to '$',
'' to '%',
'' to '^',
'' to '&',
'' to '*',
'' to '(',
'' to ')',
'' to '-',
'' to '=',
'_' to '_',
'' to '+',
'' to '[',
'' to ']',
'' to '{',
'' to '}',
';' to '',
'' to '\'',
':' to '',
'' to '\'',
',' to '',
'' to '.',
'' to '/',
'' to '<',
'' to '>',
'?' to '',
'' to '\\',
'' to '|',
'' to '`',
'' to '~',
'' to 'q',
'' to 'w',
'' to 'e',
'' to 'r',
'' to 't',
'' to 'y',
'' to 'u',
'' to 'i',
'' to 'o',
'' to 'p',
'' to 'a',
'' to 's',
'' to 'd',
'' to 'f',
'' to 'g',
'' to 'h',
'' to 'j',
'' to 'k',
'' to 'l',
'' to 'z',
'' to 'x',
'' to 'c',
'' to 'v',
'' to 'b',
'' to 'n',
'' to 'm',
'' to 'Q',
'' to 'W',
'' to 'E',
'' to 'R',
'' to 'T',
'' to 'Y',
'' to 'U',
'' to 'I',
'' to 'O',
'' to 'P',
'' to 'A',
'' to 'S',
'' to 'D',
'' to 'F',
'' to 'G',
'' to 'H',
'' to 'J',
'' to 'K',
'' to 'L',
'' to 'Z',
'' to 'X',
'' to 'C',
'' to 'V',
'' to 'B',
'' to 'N',
'' to 'M'
)

private fun normalizeContent(content: String): String {
var c = content
REPLACE_CHAR_MAP.forEach { (k, v) -> c = c.replace(k, v) }
return c
}

0 comments on commit 3815a50

Please sign in to comment.