]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Fix save_to_json
authorqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 25 Mar 2023 17:27:14 +0000 (01:27 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 25 Mar 2023 17:27:14 +0000 (01:27 +0800)
Reply/F.go
Reply/Reply.go
bili_danmu.go
demo/config/config_K_v.json

index 96663898656b3de520b7bbd447b155b90436addd..629c586b1b11a4c32a8587801ac327c8947c7dd5 100644 (file)
@@ -910,26 +910,54 @@ func Jiezouf(s []string) bool {
 }
 
 // 保存所有消息到json
-func init() {
-       Save_to_json(0, []byte{'['})
-       c.C.Danmu_Main_mq.Pull_tag(msgq.FuncMap{
-               `change_room`: func(_ interface{}) bool { //房间改变
-                       Save_to_json(0, []byte{'['})
-                       return false
-               },
-               `flash_room`: func(_ interface{}) bool { //房间改变
-                       Save_to_json(0, []byte{'['})
-                       return false
-               },
+type saveToJson struct {
+       msg  *msgq.MsgType[[]byte]
+       once sync.Once
+}
+
+func (t *saveToJson) Init() {
+       t.once.Do(func() {
+               if path, ok := c.C.K_v.LoadV(`save_to_json`).(string); ok && path != `` {
+                       f := file.New(path, 0, false)
+                       f.Delete()
+                       f.Write([]byte("["), true)
+                       f.Close()
+
+                       t.msg = msgq.NewType[[]byte]()
+                       t.msg.Pull_tag(map[string]func([]byte) (disable bool){
+                               `data`: func(b []byte) (disable bool) {
+                                       f := file.New(path, -1, false)
+                                       f.Write(b, true)
+                                       f.Write([]byte(","), true)
+                                       f.Close()
+                                       return false
+                               },
+                               `stop`: func(_ []byte) (disable bool) {
+                                       f := file.New(path, -1, false)
+                                       f.Seed(-1, 2)
+                                       f.Write([]byte("]"), true)
+                                       f.Close()
+                                       return true
+                               },
+                       })
+               }
        })
 }
 
-func Save_to_json(Loc int, context []byte) {
-       if path, ok := c.C.K_v.LoadV(`save_to_json`).(string); ok && path != `` {
-               file.New(path, int64(Loc), true).Write(context, true)
+func (t *saveToJson) Write(data []byte) {
+       if t.msg != nil {
+               t.msg.PushLock_tag(`data`, data)
        }
 }
 
+func (t *saveToJson) Close() {
+       if t.msg != nil {
+               t.msg.PushLock_tag(`stop`, nil)
+       }
+}
+
+var SaveToJson saveToJson
+
 // 进入房间发送弹幕
 func Entry_danmu() {
        flog := flog.Base_add(`进房弹幕`)
index 50f307068da750cbc1b0b391a6572c25ed87e1b1..d6857aa688cb317b7da65c9cb9a97c4a9fc2864a 100644 (file)
@@ -79,9 +79,7 @@ func Reply(b []byte) {
                switch head.OpeaT {
                case c.WS_OP_MESSAGE:
                        Msg(contain)
-                       var tmp []byte
-                       copy(tmp, contain)
-                       Save_to_json(-1, append(tmp, ','))
+                       SaveToJson.Write(contain)
                case c.WS_OP_HEARTBEAT_REPLY: //心跳响应
                        Heart(contain)
                        return //忽略剩余内容
index dd9f10d3254a47eb9ba6729e3a1072a7c4a9fb22..c0e5e5d7d937f34170d83ba961c2dcea116908ba 100644 (file)
@@ -165,6 +165,8 @@ func Start() {
                F.Dosign()
                // //客户版本 不再需要
                // F.Get(`VERSION`)
+               // 附加功能 savetojson
+               reply.SaveToJson.Init()
                //附加功能 保持牌子点亮
                go reply.Keep_medal_light()
                //附加功能 自动发送即将过期礼物
@@ -347,8 +349,7 @@ func Start() {
                                        break
                                }
                        }
-                       { //附加功能 ws信息保存
-                               reply.Save_to_json(-1, []byte("{}]"))
+                       { //附加功能
                                if v, ok := c.C.K_v.LoadV(`仅保存当前直播间流`).(bool); ok && v {
                                        reply.StreamOStop(-2) //停止其他房间录制
                                }
@@ -356,7 +357,8 @@ func Start() {
                        sys.Sys().Timeoutf(1)
                }
 
-               { //附加功能 直播流停止
+               { //附加功能 直播流停止 ws信息保存
+                       reply.SaveToJson.Close()
                        reply.StreamOStop(-1)
                }
                // close(interrupt)
index 4f6805e707f38d3a7b036526918795396b6933a8..5d4fd368bc1207d238e65c607e5348d8d8cd0475 100644 (file)
@@ -24,7 +24,7 @@
     "gtk_保留弹幕数量": 25,
     "gtk_内存头像数量": 30,
     "gtk_头像获取等待最大数量": 30,
-    "save_to_json-help": "保存消息设置,将所有消息保存为json文件,输入文件名(带后缀)",
+    "save_to_json-help": "保存消息设置,将所有消息保存为json文件,输入文件名(带后缀),启用会带来性能损耗",
     "save_to_json": "",
     "进房弹幕-help": "进入房间发送弹幕,似乎屏蔽了一些字符",
     "进房弹幕_内容": [