From 2589fae3c50b8a62c432dc46bd42365aa6e9f2d8 Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Sun, 26 Mar 2023 01:27:14 +0800 Subject: [PATCH] Fix save_to_json --- Reply/F.go | 56 +++++++++++++++++++++++++++---------- Reply/Reply.go | 4 +-- bili_danmu.go | 8 ++++-- demo/config/config_K_v.json | 2 +- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/Reply/F.go b/Reply/F.go index 9666389..629c586 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -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(`进房弹幕`) diff --git a/Reply/Reply.go b/Reply/Reply.go index 50f3070..d6857aa 100644 --- a/Reply/Reply.go +++ b/Reply/Reply.go @@ -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 //忽略剩余内容 diff --git a/bili_danmu.go b/bili_danmu.go index dd9f10d..c0e5e5d 100644 --- a/bili_danmu.go +++ b/bili_danmu.go @@ -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) diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index 4f6805e..5d4fd36 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -24,7 +24,7 @@ "gtk_保留弹幕数量": 25, "gtk_内存头像数量": 30, "gtk_头像获取等待最大数量": 30, - "save_to_json-help": "保存消息设置,将所有消息保存为json文件,输入文件名(带后缀)", + "save_to_json-help": "保存消息设置,将所有消息保存为json文件,输入文件名(带后缀),启用会带来性能损耗", "save_to_json": "", "进房弹幕-help": "进入房间发送弹幕,似乎屏蔽了一些字符", "进房弹幕_内容": [ -- 2.39.2