-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_eventtab.go
158 lines (125 loc) · 3.64 KB
/
gen_eventtab.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func genEventTab(ctx *CommandContext) {
code := &bytes.Buffer{}
// 生成注释
{
program := strings.TrimSuffix(filepath.Base(os.Args[0]), filepath.Ext(os.Args[0]))
args := strings.Join(os.Args[1:], " ")
fmt.Fprintf(code, `// Code generated by %s %s; DO NOT EDIT.
package %s
`, program, args, ctx.EventTabPackage)
}
// 生成import
{
importCode := &bytes.Buffer{}
fmt.Fprintf(importCode, "\nimport (")
fmt.Fprintf(importCode, `
%s "%s"`, ctx.PackageEventAlias, packageEventPath)
fmt.Fprintf(importCode, "\n)\n")
fmt.Fprintf(code, importCode.String())
}
// 解析事件定义
eventDeclTab := EventDeclTab{}
eventDeclTab.Parse(ctx)
// event包前缀
eventPrefix := ""
if ctx.PackageEventAlias != "." {
eventPrefix = ctx.PackageEventAlias + "."
}
// 生成事件表接口
{
var eventsCode string
for _, event := range eventDeclTab {
eventsCode += fmt.Sprintf("\t%s() %sIEvent\n", event.Name, eventPrefix)
}
fmt.Fprintf(code, `
type I%[1]s interface {
%[2]s}
`, strings.Title(ctx.EventTabName), eventsCode)
}
// 生成事件表
{
var eventsRecursionCode string
for i, event := range eventDeclTab {
var eventRecursion string
if strings.Contains(event.Comment, "[EventRecursion_Allow]") {
eventRecursion = eventPrefix + "EventRecursion_Allow"
} else if strings.Contains(event.Comment, "[EventRecursion_Disallow]") {
eventRecursion = eventPrefix + "EventRecursion_Disallow"
} else if strings.Contains(event.Comment, "[EventRecursion_Discard]") {
eventRecursion = eventPrefix + "EventRecursion_Discard"
} else if strings.Contains(event.Comment, "[EventRecursion_Truncate]") {
eventRecursion = eventPrefix + "EventRecursion_Truncate"
} else if strings.Contains(event.Comment, "[EventRecursion_Deepest]") {
eventRecursion = eventPrefix + "EventRecursion_Deepest"
} else {
eventRecursion = "recursion"
}
eventsRecursionCode += fmt.Sprintf("\t(*eventTab)[%d].Init(autoRecover, reportError, %s)\n", i, eventRecursion)
}
// 生成事件Id
{
fmt.Fprintln(code, `
var (`)
fmt.Fprintf(code, ` _%[1]sId = %[2]sDeclareEventTabIdT[%[1]s]()
`, ctx.EventTabName, eventPrefix)
for i, event := range eventDeclTab {
fmt.Fprintf(code, ` %[2]sId = _%[1]sId + %[3]d
`, ctx.EventTabName, event.Name, i)
}
fmt.Fprintln(code, ")")
}
fmt.Fprintf(code, `
type %[1]s [%[2]d]%[4]sEvent
func (eventTab *%[1]s) Init(autoRecover bool, reportError chan error, recursion %[4]sEventRecursion) {
%[3]s}
func (eventTab *%[1]s) Get(id uint64) %[4]sIEvent {
if _%[1]sId != id & 0xFFFFFFFF00000000 {
return nil
}
pos := id & 0xFFFFFFFF
if pos >= uint64(len(*eventTab)) {
return nil
}
return &(*eventTab)[pos]
}
func (eventTab *%[1]s) Open() {
for i := range *eventTab {
(*eventTab)[i].Open()
}
}
func (eventTab *%[1]s) Close() {
for i := range *eventTab {
(*eventTab)[i].Close()
}
}
func (eventTab *%[1]s) Clean() {
for i := range *eventTab {
(*eventTab)[i].Clean()
}
}
`, ctx.EventTabName, len(eventDeclTab), eventsRecursionCode, eventPrefix)
}
for i, event := range eventDeclTab {
fmt.Fprintf(code, `
func (eventTab *%[1]s) %[2]s() %[4]sIEvent {
return &(*eventTab)[%[3]d]
}
`, ctx.EventTabName, event.Name, i, eventPrefix)
}
fmt.Printf("EventTab: %s\n", ctx.EventTabName)
// 目标文件
targetFile := filepath.Join(filepath.Dir(ctx.DeclFile), ctx.EventTabDir, filepath.Base(strings.TrimSuffix(ctx.DeclFile, ".go"))+"_tab_code.go")
os.MkdirAll(filepath.Dir(targetFile), os.ModePerm)
if err := ioutil.WriteFile(targetFile, code.Bytes(), os.ModePerm); err != nil {
panic(err)
}
}