forked from tamilpp25/Iridium-SR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_service.go
97 lines (79 loc) · 1.9 KB
/
proto_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/jhump/protoreflect/dynamic"
)
var msgMap = make(map[string]*desc.MessageDescriptor)
var packetIdMap map[uint16]string
var packetNameMap = make(map[string]uint16)
var protoParser = protoparse.Parser{}
func InitProto() {
packetIdFile, _ := ioutil.ReadFile("./data/packetIds.json")
err := json.Unmarshal(packetIdFile, &packetIdMap)
if err != nil {
log.Fatalln("Could not load ./data/packetIds.json", err)
}
for k, v := range packetIdMap {
packetNameMap[v] = k
}
protoParser.ImportPaths = []string{"./data/"}
LoadProto("StarRail")
}
func LoadProto(protoName string) {
fileDesc, err := protoParser.ParseFiles(protoName + ".proto")
if err != nil {
log.Println("Could not load proto file", protoName, err)
return
}
for _, msg := range fileDesc[0].GetMessageTypes() {
msgMap[msg.GetName()] = msg
}
}
func GetProtoById(id uint16) *desc.MessageDescriptor {
protoName, ok := packetIdMap[id]
if !ok {
return nil
}
return msgMap[protoName]
}
func GetProtoNameById(id uint16) string {
protoName, ok := packetIdMap[id]
if !ok {
return ""
}
return protoName
}
func parseProto(id uint16, data []byte) (*dynamic.Message, error) {
msg := GetProtoById(id)
if msg == nil {
return nil, errors.New("not found")
}
dMsg := dynamic.NewMessage(msg)
err := dMsg.Unmarshal(data)
return dMsg, err
}
func parseProtoToJson(id uint16, data []byte) string {
dMsg, err := parseProto(id, data)
if err != nil {
return ""
}
marshalJSON, err := dMsg.MarshalJSON()
if err != nil {
return ""
}
return string(marshalJSON)
}
func parseProtoToInterface(id uint16, data []byte) *interface{} {
object := parseProtoToJson(id, data)
var result *interface{}
err := json.Unmarshal([]byte(object), &result)
if err != nil {
return nil
}
return result
}