func GetItemInfo(obj datatype.Entityer, syncchild bool) (*datatype.EntityInfo, error) { item := &datatype.EntityInfo{} buffer := new(bytes.Buffer) enc := gob.NewEncoder(buffer) err := enc.Encode(obj) if err != nil { log.LogError("encode ", obj.ObjTypeName(), "error,", err) return nil, err } item.Type = obj.ObjTypeName() item.Caps = obj.GetCapacity() item.DbId = obj.GetDbId() item.ObjId = obj.GetObjId() item.Index = obj.GetIndex() item.Data = buffer.Bytes() if syncchild { ls := obj.GetChilds() if len(ls) > 0 { item.Childs = make([]*datatype.EntityInfo, 0, len(ls)) } for _, c := range ls { if c != nil { child, err := GetItemInfo(c, syncchild) if err != nil { return nil, err } item.Childs = append(item.Childs, child) } } } return item, nil }
func GetEntityData(ent datatype.Entityer, base bool, depth int) *SaveEntity { if !base && !ent.IsSave() { return nil } s := &SaveEntity{} ent.SyncToDb() s.Typ = ent.ObjTypeName() s.DBId = ent.GetDbId() s.Obj = ent.GetSaveLoader() s.Index = ent.GetIndex() if ent.Base() != nil { s.Base = GetEntityData(ent.Base(), true, depth) } if base { return s } if depth > 1 { return s } clds := ent.GetChilds() l := len(clds) if l > 0 { s.Childs = make([]*SaveEntity, 0, l) for _, e := range clds { if e != nil { if child := GetEntityData(e, false, depth+1); child != nil { s.Childs = append(s.Childs, child) } } } } return s }
//释放一个具体对象,如果池中有空间,则回收,如果空闲的超过一定数量,则不回收,直接删除 func (p *Pool) Free(e datatype.Entityer) { if l, exist := p.pool[e.ObjTypeName()]; exist { if l.Len() <= MAX_POOL_FREE { e.Reset() l.PushBack(e) } } else { l := list.New() e.Reset() l.PushBack(e) p.pool[e.ObjTypeName()] = l } }