]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Improve 表情保存 (#145)
authorqydysky <qydysky@foxmail.com>
Thu, 9 Jan 2025 11:46:57 +0000 (19:46 +0800)
committerGitHub <noreply@github.com>
Thu, 9 Jan 2025 11:46:57 +0000 (19:46 +0800)
Reply/F.go
Reply/F/comp.go
Reply/F/danmuEmotes/danmuEmotes.go
Reply/Reply.go

index 1819727ad9c585b90f99cafc99a8a33231078081..0871382501808bfda76ec67b34aec0e3323ad4f9 100644 (file)
@@ -29,7 +29,6 @@ import (
        c "github.com/qydysky/bili_danmu/CV"
        F "github.com/qydysky/bili_danmu/F"
        replyFunc "github.com/qydysky/bili_danmu/Reply/F"
-       danmuemotes "github.com/qydysky/bili_danmu/Reply/F/danmuEmotes"
        "github.com/qydysky/bili_danmu/Reply/F/danmuXml"
        "github.com/qydysky/bili_danmu/Reply/F/keepMedalLight"
        videoInfo "github.com/qydysky/bili_danmu/Reply/F/videoInfo"
@@ -1012,7 +1011,7 @@ func init() {
                        }
 
                        if strings.HasPrefix(p, "emots/") {
-                               p, _ = danmuemotes.Hashr.Run(context.Background(), p)
+                               p = replyFunc.DanmuEmotes.Hashr(p)
                        } else {
                                p = "html/artPlayer/" + p
                        }
index f6a4a7b6b9000d364944c85c23e5f3ae755a1522..7add94a535ef5a38115408e1094ad8dd2669f08e 100644 (file)
@@ -7,8 +7,10 @@ import (
 
        _ "github.com/qydysky/bili_danmu/Reply/F/ass"
        _ "github.com/qydysky/bili_danmu/Reply/F/danmuCountPerMin"
+       _ "github.com/qydysky/bili_danmu/Reply/F/danmuEmotes"
        _ "github.com/qydysky/bili_danmu/Reply/F/danmuji"
        comp "github.com/qydysky/part/component2"
+       log "github.com/qydysky/part/log"
 )
 
 var DanmuCountPerMin = comp.Get[interface {
@@ -27,3 +29,19 @@ var Danmuji = comp.Get[interface {
        Danmujif(s string, then func(string))
        Danmuji_auto(ctx context.Context, danmus []any, waitSec float64, then func(string))
 }](`danmuji`)
+
+type DanmuEmotesS struct {
+       Logg *log.Log_interface
+       Info []any
+       Msg  *string
+}
+
+var DanmuEmotes = comp.Get[interface {
+       SaveEmote(ctx context.Context, ptr struct {
+               Logg *log.Log_interface
+               Info []any
+               Msg  *string
+       }) (ret any, err error)
+       Hashr(s string) (r string)
+       SetLayerN(n int)
+}](`danmuEmotes`)
index bca94ee1f9c4735f6a1bda404568cdb67cbe52cc..d4fc357c855168a5a9047932ee7f9004c977936f 100644 (file)
@@ -6,47 +6,48 @@ import (
        "strings"
 
        c "github.com/qydysky/bili_danmu/CV"
-       comp "github.com/qydysky/part/component"
+       comp "github.com/qydysky/part/component2"
        file "github.com/qydysky/part/file"
        phash "github.com/qydysky/part/hash"
        log "github.com/qydysky/part/log"
        reqf "github.com/qydysky/part/reqf"
 )
 
-// path
-var (
-       SaveEmote = comp.NewComp(saveEmote)
-       Hashr     = comp.NewComp(func(ctx context.Context, s string) (r string, e error) {
-               return hashr(s), nil
-       })
-       // danmuPool = ppool.New(ppool.PoolFunc[pslice.Buf[byte]]{
-       //      New: func() *pslice.Buf[byte] {
-       //              return pslice.New[byte]()
-       //      },
-       //      InUse: func(b *pslice.Buf[byte]) bool {
-       //              return !b.IsEmpty()
-       //      },
-       //      Reuse: func(b *pslice.Buf[byte]) *pslice.Buf[byte] {
-       //              return b
-       //      },
-       //      Pool: func(b *pslice.Buf[byte]) *pslice.Buf[byte] {
-       //              b.Reset()
-       //              return b
-       //      },
-       // }, 100)
-)
-
-type Danmu struct {
-       Logg *log.Log_interface
-       Info []any
-       Msg  *string
+type TargetInterface interface {
+       SaveEmote(ctx context.Context, ptr struct {
+               Logg *log.Log_interface
+               Info []any
+               Msg  *string
+       }) (ret any, err error)
+       Hashr(s string) (r string)
+       SetLayerN(n int)
 }
 
 func init() {
-       _, _ = file.New("emots/README.md", 0, true).Write([]byte(""), false)
+       i := danmuEmotes{
+               Dir:    "emots/",
+               LayerN: 0,
+       }
+       if e := comp.Register[TargetInterface]("danmuEmotes", &i); e != nil {
+               panic(e)
+       }
+       _, _ = file.New(i.Dir+"README.md", 0, true).Write([]byte(""), false)
 }
 
-func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) {
+type danmuEmotes struct {
+       Dir    string
+       LayerN int
+}
+
+func (t *danmuEmotes) SetLayerN(n int) {
+       t.LayerN = n
+}
+
+func (t *danmuEmotes) SaveEmote(ctx context.Context, ptr struct {
+       Logg *log.Log_interface
+       Info []any
+       Msg  *string
+}) (ret any, err error) {
        if m, ok := ptr.Info[13].(map[string]any); ok {
                if url, ok := m[`url`].(string); ok {
                        if !strings.Contains(*ptr.Msg, "[") {
@@ -54,7 +55,7 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) {
                                        *ptr.Msg = "[" + *ptr.Msg + emoticon_unique + "]"
                                }
                        }
-                       savePath := "emots/" + hashr(*ptr.Msg) + ".png"
+                       savePath := t.Dir + t.Hashr(*ptr.Msg) + ".png"
                        if !file.New(savePath, 0, true).IsExist() {
                                go func() {
                                        req := c.C.ReqPool.Get()
@@ -100,7 +101,7 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) {
                                                        continue
                                                }
 
-                                               savePath := "emots/" + hashr(k) + ".png"
+                                               savePath := t.Dir + t.Hashr(k) + ".png"
                                                if file.New(savePath, 0, true).IsExist() {
                                                        continue
                                                }
@@ -134,45 +135,19 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) {
        return
 }
 
-func hashr(s string) (r string) {
-       return phash.Md5String(s)
-
-       // buf := danmuPool.Get()
-       // defer danmuPool.Put(buf)
-
-       // emoteB := false
-       // for i := 0; i < len(s); i++ {
-       //      if !emoteB {
-       //              _ = buf.Append([]byte{s[i]})
-       //              emoteB = s[i] == '['
-       //              continue
-       //      } else if s[i] == ']' {
-       //              _ = buf.Append([]byte{s[i]})
-       //              emoteB = false
-       //              continue
-       //      }
-       //      switch s[i] {
-       //      case '\\':
-       //              _ = buf.Append([]byte("_1"))
-       //      case '/':
-       //              _ = buf.Append([]byte("_2"))
-       //      case '*':
-       //              _ = buf.Append([]byte("_3"))
-       //      case '<':
-       //              _ = buf.Append([]byte("_4"))
-       //      case '>':
-       //              _ = buf.Append([]byte("_5"))
-       //      case '|':
-       //              _ = buf.Append([]byte("_6"))
-       //      case ':':
-       //              _ = buf.Append([]byte(":"))
-       //      case '?':
-       //              _ = buf.Append([]byte("?"))
-       //      case '"':
-       //              _ = buf.Append([]byte("“"))
-       //      default:
-       //              _ = buf.Append([]byte{s[i]})
-       //      }
-       // }
-       // return string(buf.GetCopyBuf())
+func (t *danmuEmotes) Hashr(s string) (r string) {
+       rs := phash.Md5String(s)
+       rr := []byte{}
+       layer := t.LayerN
+       if layer > len(rs)-1 {
+               layer = layer - 1
+       }
+       for i := 0; i < len(rs); i++ {
+               rr = append(rr, rs[i])
+               if layer > 0 {
+                       rr = append(rr, '/')
+                       layer -= 1
+               }
+       }
+       return string(rr)
 }
index 1154b96bebe4a803a5f0b1fa793687a2b7dda16e..7472d6aa2cbcd935d7bfa43d5868cca88251cc16 100644 (file)
@@ -15,7 +15,6 @@ import (
        c "github.com/qydysky/bili_danmu/CV"
        F "github.com/qydysky/bili_danmu/F"
        replyFunc "github.com/qydysky/bili_danmu/Reply/F"
-       danmuemotes "github.com/qydysky/bili_danmu/Reply/F/danmuEmotes"
        "github.com/qydysky/bili_danmu/Reply/F/danmuReLiveTriger"
        "github.com/qydysky/bili_danmu/Reply/F/liveOver"
        "github.com/qydysky/bili_danmu/Reply/F/recStartEnd"
@@ -1290,7 +1289,7 @@ func (t replyF) danmu(s string) {
                        item.color = "#" + fmt.Sprintf("%x", F.Itob32(int32(i[3].(float64)))[1:])
 
                        if v, ok := t.Common.K_v.LoadV(`弹幕表情`).(bool); ok && v {
-                               if _, e := danmuemotes.SaveEmote.Run(context.Background(), danmuemotes.Danmu{Logg: msglog, Info: i, Msg: &item.msg}); e != nil {
+                               if _, e := replyFunc.DanmuEmotes.SaveEmote(context.Background(), replyFunc.DanmuEmotesS{Logg: msglog, Info: i, Msg: &item.msg}); e != nil {
                                        msglog.L(`E: `, e)
                                }
                        }