github.com/metaworking/channeld@v0.7.3/pkg/unrealpb/extension.go (about) 1 package unrealpb 2 3 import ( 4 "errors" 5 6 "github.com/metaworking/channeld/pkg/channeld" 7 "github.com/metaworking/channeld/pkg/channeldpb" 8 "github.com/metaworking/channeld/pkg/common" 9 ) 10 11 // Implement [channeld.HandoverDataWithPayload] 12 func (data *HandoverData) ClearPayload() { 13 data.ChannelData = nil 14 } 15 16 // Implement [channeld.ChannelDataInitializer] 17 func (data *SpatialChannelData) Init() error { 18 data.Entities = make(map[uint32]*SpatialEntityState) 19 return nil 20 } 21 22 // Implement [channeld.MergeableChannelData] 23 func (dst *SpatialChannelData) Merge(src common.ChannelDataMessage, options *channeldpb.ChannelDataMergeOptions, spatialNotifier common.SpatialInfoChangedNotifier) error { 24 srcData, ok := src.(*SpatialChannelData) 25 if !ok { 26 return errors.New("src is not a SpatialChannelData") 27 } 28 29 for netId, entity := range srcData.Entities { 30 if entity.Removed { 31 delete(dst.Entities, netId) 32 33 entityCh := channeld.GetChannel(common.ChannelId(netId)) 34 if entityCh != nil { 35 entityCh.Logger().Info("removing entity channel from SpatialChannelData.Merge()") 36 channeld.RemoveChannel(entityCh) 37 } 38 } else { 39 // Do not merge the SpatialEntityState if it already exists in the channel data 40 if _, exists := dst.Entities[netId]; !exists { 41 dst.Entities[netId] = entity 42 } 43 } 44 } 45 46 return nil 47 } 48 49 // Entity channel data should implement this interface 50 type EntityChannelDataWithObjRef interface { 51 GetObjRef() *UnrealObjectRef 52 } 53 54 // Implement [channeld.SpatialChannelDataUpdater] 55 func (dst *SpatialChannelData) AddEntity(entityId channeld.EntityId, msg common.Message) error { 56 entityData, ok := msg.(EntityChannelDataWithObjRef) 57 if !ok { 58 return errors.New("msg is doesn't have GetObjRef()") 59 } 60 61 dst.Entities[uint32(entityId)] = &SpatialEntityState{ 62 ObjRef: entityData.GetObjRef(), 63 } 64 65 return nil 66 } 67 68 func (dst *SpatialChannelData) RemoveEntity(entityId channeld.EntityId) error { 69 delete(dst.Entities, uint32(entityId)) 70 return nil 71 }