-
Notifications
You must be signed in to change notification settings - Fork 43
/
EntityProtoType.go
67 lines (55 loc) · 1.5 KB
/
EntityProtoType.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
package entity
import (
"reflect"
"zeus/global"
"zeus/iserver"
log "github.com/cihub/seelog"
)
// ProtoType 保存实体映射列表
type ProtoType struct {
entityProtoType map[string]reflect.Type
}
// NewProtoType 创建新的对象
func NewProtoType() *ProtoType {
return &ProtoType{
entityProtoType: make(map[string]reflect.Type),
}
}
// RegProtoType 注册原型
func (es *ProtoType) RegProtoType(name string, protoType iserver.IEntity, autoCreate bool) {
if protoType == nil {
log.Error("ProtoType is nil", name)
return
}
_, ok := es.entityProtoType[name]
if ok {
log.Warn("Registed type", name)
return
}
t := reflect.TypeOf(protoType)
es.entityProtoType[name] = t
// only not space entity should be register to server type list
//if _, ok := protoType.(iserver.ICellEntity); !ok {
if autoCreate {
// dbservice.EntityTypeUtil(name).RegSrvType(iserver.GetSrvInst().GetSrvType())
types := global.GetGlobalInst().GetGlobalIntSlice("EntitySrvTypes:" + name)
curType := iserver.GetSrvInst().GetSrvType()
for _, typ := range types {
if typ == int(curType) {
return
}
}
types = append(types, int(curType))
global.GetGlobalInst().SetGlobalIntSlice("EntitySrvTypes:"+name, types)
}
}
// NewEntityByProtoType 创建 一个对象
func (es *ProtoType) NewEntityByProtoType(entityType string) interface{} {
proto, ok := es.entityProtoType[entityType]
if !ok {
log.Error("Entity type not exist", entityType)
return nil
}
e := reflect.New(proto.Elem()).Interface()
return e
}