forked from paralin/go-dota2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_packages.go
86 lines (73 loc) · 1.83 KB
/
proto_packages.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
package main
import (
"go/importer"
"go/token"
"go/types"
"regexp"
)
// TypeRegex matches struct types in Go
var TypeRegex = regexp.MustCompile("(type)(.+)(struct{)(.+)(})")
// ProtoType represents a protobuf message type with some heuristics.
type ProtoType struct {
// PakStr is the containing package import string
PakStr string
// Pak is the containing package.
Pak *types.Package
// TypeName is the name of the type, like CSODOTALobby
TypeName string
// Obj is the object.
Obj types.Object
// TypeStr is the fully qualified type string.
TypeStr string
}
// BuildProtoTypeMap attempts to compile the proto type list.
func BuildProtoTypeMap() (map[string]*types.Package, map[string]*ProtoType, error) {
packages := make(map[string]*types.Package)
// imp := importer.Default()
fset := token.NewFileSet()
imp := importer.ForCompiler(fset, "source", nil)
importPak := func(pak string) error {
ipak, err := imp.Import(pak)
if err != nil {
return err
}
packages[pak] = ipak
return nil
}
// import all packages
allPackages := []string{
"github.com/paralin/go-dota2/protocol",
}
for _, pak := range allPackages {
if err := importPak(pak); err != nil {
return nil, nil, err
}
}
if err := importPak("github.com/faceit/go-steam/steamid"); err != nil {
return nil, nil, err
}
// build a map of proto types
protoMap := make(map[string]*ProtoType)
for pakStr, pak := range packages {
scope := pak.Scope()
for _, nam := range scope.Names() {
obj := scope.Lookup(nam)
objStr := obj.String()
if !obj.Exported() {
continue
}
if !TypeRegex.MatchString(objStr) {
continue
}
protoTyp := &ProtoType{
PakStr: pakStr,
Pak: pak,
TypeName: obj.Name(),
Obj: obj,
TypeStr: obj.String(),
}
protoMap[obj.Name()] = protoTyp
}
}
return packages, protoMap, nil
}