From 72f0e29c3a012ecc945759e21157e02ceb781361 Mon Sep 17 00:00:00 2001 From: Akiba Date: Sat, 4 Jun 2022 09:16:58 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20":art:=20parse=20=E5=87=8F=E5=B0=91=20?= =?UTF-8?q?gjson=20=E4=BD=BF=E7=94=A8=20=E4=BC=98=E5=8C=96=E6=80=A7?= =?UTF-8?q?=E8=83=BD"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit b4bc1a280f301d4303957fb7d2f19633a2ec763e. --- message/gift.go | 8 +++++--- message/guard.go | 8 +++++--- message/superchat.go | 7 +++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/message/gift.go b/message/gift.go index 2950696..dcbd0b7 100644 --- a/message/gift.go +++ b/message/gift.go @@ -1,8 +1,10 @@ package message import ( + "bytes" "encoding/json" log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" ) type Gift struct { @@ -109,9 +111,9 @@ type ComboSend struct { } func (g *Gift) Parse(data []byte) { - // len("{"cmd":"","data":") == 17 , len('SEND_GIFT') = 9 - d := data[17+9 : len(data)-1] - err := json.Unmarshal(d, g) + sb := bytes.NewBuffer(data).String() + sd := gjson.Get(sb, "data").String() + err := json.Unmarshal([]byte(sd), g) if err != nil { log.Error("parse Gift failed") } diff --git a/message/guard.go b/message/guard.go index b1ef303..4f2f0a6 100644 --- a/message/guard.go +++ b/message/guard.go @@ -1,8 +1,10 @@ package message import ( + "bytes" "encoding/json" log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" ) type GuardBuy struct { @@ -18,9 +20,9 @@ type GuardBuy struct { } func (g *GuardBuy) Parse(data []byte) { - // len("{"cmd":"","data":") = 17 , len('GUARD_BUY') = 9 - d := data[17+9 : len(data)-1] - err := json.Unmarshal(d, g) + sb := bytes.NewBuffer(data).String() + sd := gjson.Get(sb, "data").String() + err := json.Unmarshal([]byte(sd), g) if err != nil { log.Error("parse GuardBuy failed") } diff --git a/message/superchat.go b/message/superchat.go index c6a88f8..6596bad 100644 --- a/message/superchat.go +++ b/message/superchat.go @@ -1,8 +1,10 @@ package message import ( + "bytes" "encoding/json" log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" ) // SuperChat @@ -70,8 +72,9 @@ type SuperChat struct { } func (s *SuperChat) Parse(data []byte) { - // len('{"cmd":"","data":') = 17 , len('SUPER_CHAT_MESSAGE') = 18 - err := json.Unmarshal(data[17+18:len(data)-1], s) + sb := bytes.NewBuffer(data).String() + sd := gjson.Get(sb, "data").String() + err := json.Unmarshal([]byte(sd), s) if err != nil { log.Error("parse superchat failed") }