forked from davyxu/tabtoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_gen.go
292 lines (212 loc) · 4.83 KB
/
table_gen.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Generated by github.com/davyxu/tabtoy
// Version: 2.9.0
// DO NOT EDIT!!
package table
import (
"fmt"
"encoding/json"
"io/ioutil"
)
// Defined in table: Globals
type ActorType int32
const (
// 唐僧
ActorType_Leader ActorType = 0
// 孙悟空
ActorType_Monkey ActorType = 1
// 猪八戒
ActorType_Pig ActorType = 2
// 沙僧
ActorType_Hammer ActorType = 3
)
var (
ActorTypeMapperValueByName = map[string]int32{
"Leader": 0,
"Monkey": 1,
"Pig": 2,
"Hammer": 3,
}
ActorTypeMapperNameByValue = map[int32]string{
0: "Leader",
1: "Monkey",
2: "Pig",
3: "Hammer",
}
ActorTypeCommentByValue = map[int32]string{
0: "唐僧",
1: "孙悟空",
2: "猪八戒",
3: "沙僧",
}
)
func (self ActorType) String() string {
name, _ := ActorTypeMapperNameByValue[int32(self)]
return name
}
// Defined in table: Config
type Config struct {
//Sample
Sample []*SampleDefine
}
// Defined in table: Globals
type Vec2 struct {
X int32
Y int32
}
// Defined in table: Sample
type Prop struct {
// 血量
HP int32
// 攻击速率
AttackRate float32
// 额外类型
ExType ActorType
}
// Defined in table: Sample
type AttackParam struct {
// 攻击值
Value int32
}
// Defined in table: Sample
type SampleDefine struct {
//唯一ID
ID int64
//名称
Name string `自定义tag:"支持go的struct tag"`
EmptyName string
//图标ID
IconID int32
//攻击率
NumericalRate float32
//物品id
ItemID int32
//BuffID
BuffID []int32
//位置
Pos *Vec2
//类型
Type ActorType
//技能ID列表
SkillID []int32
//攻击参数
AttackParam *AttackParam
//单结构解析
SingleStruct *Prop
//字符串结构
StrStruct []*Prop
}
// Config 访问接口
type ConfigTable struct {
// 表格原始数据
Config
// 索引函数表
indexFuncByName map[string][]func(*ConfigTable) error
// 清空函数表
clearFuncByName map[string][]func(*ConfigTable) error
// 加载前回调
preFuncList []func(*ConfigTable) error
// 加载后回调
postFuncList []func(*ConfigTable) error
SampleByID map[int64]*SampleDefine
SampleByName map[string]*SampleDefine
}
// 从json文件加载
func (self *ConfigTable) Load(filename string) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return self.LoadData(data)
}
// 从二进制加载
func (self *ConfigTable) LoadData(data []byte) error {
var newTab Config
// 读取
err := json.Unmarshal(data, &newTab)
if err != nil {
return err
}
// 所有加载前的回调
for _, v := range self.preFuncList {
if err = v(self); err != nil {
return err
}
}
// 清除前通知
for _, list := range self.clearFuncByName {
for _, v := range list {
if err = v(self); err != nil {
return err
}
}
}
// 复制数据
self.Config = newTab
// 生成索引
for _, list := range self.indexFuncByName {
for _, v := range list {
if err = v(self); err != nil {
return err
}
}
}
// 所有完成时的回调
for _, v := range self.postFuncList {
if err = v(self); err != nil {
return err
}
}
return nil
}
// 注册外部索引入口, 索引回调, 清空回调
func (self *ConfigTable) RegisterIndexEntry(name string, indexCallback func(*ConfigTable) error, clearCallback func(*ConfigTable) error) {
indexList, _ := self.indexFuncByName[name]
clearList, _ := self.clearFuncByName[name]
if indexCallback != nil {
indexList = append(indexList, indexCallback)
}
if clearCallback != nil {
clearList = append(clearList, clearCallback)
}
self.indexFuncByName[name] = indexList
self.clearFuncByName[name] = clearList
}
// 注册加载前回调
func (self *ConfigTable) RegisterPreEntry(callback func(*ConfigTable) error) {
self.preFuncList = append(self.preFuncList, callback)
}
// 注册所有完成时回调
func (self *ConfigTable) RegisterPostEntry(callback func(*ConfigTable) error) {
self.postFuncList = append(self.postFuncList, callback)
}
// 创建一个Config表读取实例
func NewConfigTable() *ConfigTable {
return &ConfigTable{
indexFuncByName: map[string][]func(*ConfigTable) error{
"Sample": {func(tab *ConfigTable) error {
// Sample
for _, def := range tab.Sample {
if _, ok := tab.SampleByID[def.ID]; ok {
panic(fmt.Sprintf("duplicate index in SampleByID: %v", def.ID))
}
if _, ok := tab.SampleByName[def.Name]; ok {
panic(fmt.Sprintf("duplicate index in SampleByName: %v", def.Name))
}
tab.SampleByID[def.ID] = def
tab.SampleByName[def.Name] = def
}
return nil
}},
},
clearFuncByName: map[string][]func(*ConfigTable) error{
"Sample": {func(tab *ConfigTable) error {
// Sample
tab.SampleByID = make(map[int64]*SampleDefine)
tab.SampleByName = make(map[string]*SampleDefine)
return nil
}},
},
SampleByID: make(map[int64]*SampleDefine),
SampleByName: make(map[string]*SampleDefine),
}
}