Пример #1
0
    private void RemovedEntities()
    {
        EntityArray entities = removedSyncEntities.entities;
        ComponentDataArray <NetworkSyncState> networkSyncs = removedSyncEntities.networkSyncStateComponents;

        for (int i = 0; i < entities.Length; i++)
        {
            NetworkSyncState component = new NetworkSyncState()
            {
                actorId   = networkManager.LocalPlayerID,
                networkId = networkManager.GetNetworkId(),
            };
            PostUpdateCommands.RemoveComponent <NetworkSyncState>(entities[i]);
            for (int j = 0; j < RemoveComponentOnDestroyEntityMethods.Count; j++)
            {
                RemoveComponentOnDestroyEntityMethods[j].Invoke(this, entities[i]);
            }

            NetworkSyncEntity networkSyncEntity = new NetworkSyncEntity {
                ActorId   = component.actorId,
                NetworkId = component.networkId,
            };
            ownNetworkSendMessageUtility.RemoveEntity(networkSyncEntity);
            AllNetworkSendMessageUtility.RemoveEntity(networkSyncEntity);
        }
    }
Пример #2
0
    private void AddedComponents <T>() where T : struct, IComponentData
    {
        ComponentType          componentType = ComponentType.Create <T>();
        ComponentGroup         group         = GetComponentGroup(ComponentType.Create <NetworkSyncState>(), componentType, ComponentType.Subtractive <NetworkComponentState <T> >(), ComponentType.Create <NetworktAuthority>());
        ComponentDataArray <T> components    = group.GetComponentDataArray <T>();
        ComponentDataArray <NetworkSyncState> networkSyncStateComponents = group.GetComponentDataArray <NetworkSyncState>();
        EntityArray entities = group.GetEntityArray();

        NetworkMemberInfo[] networkMemberInfos = reflectionUtility.GetNetworkMemberInfo(componentType);

        for (int i = 0; i < entities.Length; i++)
        {
            NetworkSyncState       networkSyncState = networkSyncStateComponents[i];
            ComponentDataContainer componentData    = new ComponentDataContainer {
                ComponentTypeId = reflectionUtility.GetComponentTypeID(componentType)
            };

            T component = components[i];
            for (int j = 0; j < networkMemberInfos.Length; j++)
            {
                componentData.MemberData.Add(new MemberDataContainer {
                    MemberId = j,
                    Data     = (networkMemberInfos[j] as NetworkMemberInfo <T>).GetValue(component),
                });
            }


            ownNetworkSendMessageUtility.AddComponent(entities[i], networkSyncState.actorId, networkSyncState.networkId, componentData);
            AllNetworkSendMessageUtility.AddComponent(entities[i], networkSyncState.actorId, networkSyncState.networkId, componentData);

            int numberOfMembers = reflectionUtility.GetNumberOfMembers(componentType.GetManagedType());
            networkFactory.CreateNetworkComponentData <T>(entities[i], numberOfMembers);
            PostUpdateCommands.AddComponent(entities[i], new NetworkComponentState <T>());
        }
    }
    public override int NativeToInteger(object value, ComponentDataFromEntity <NetworkSyncState> networkSyncStateEntities)
    {
        Entity           entity            = (Entity)value;
        NetworkSyncState networkSynchState = networkSyncStateEntities[entity];

        return(NetworkUtility.GetNetworkEntityHash(networkSynchState.actorId, networkSynchState.networkId));
    }
Пример #4
0
            // Delegates used to know when a player connects / disconnects to add / remove it's synk state

            // When the player first connects with someone else we start the ping coroutine
            void OnPlayerConnectionConfirmed(string guid)
            {
                if (guid != UnityEngine.Network.player.guid)
                {
                    NetworkSyncState syncState = new NetworkSyncState();
                    if (syncStates == null)
                    {
                        syncStates = new Dictionary <string, NetworkSyncState>();
                    }
                    syncStates.Add(guid, syncState);
                    if (syncStates.Count == 1)
                    {
                        StopCoroutine(PingPeers());
                        StartCoroutine(PingPeers());
                    }
                }
            }
Пример #5
0
    private void UpdateDataState <T>() where T : struct, IComponentData
    {
        ComponentType  componentType = ComponentType.Create <T>();
        ComponentGroup group         = GetComponentGroup(ComponentType.Create <NetworkSyncState>(), componentType, ComponentType.Create <NetworkComponentState <T> >(), ComponentType.Create <NetworktAuthority>());
        ComponentDataArray <NetworkSyncState> networkSyncStateComponents = group.GetComponentDataArray <NetworkSyncState>();
        ComponentDataArray <T> networkComponents = group.GetComponentDataArray <T>();
        ComponentDataArray <NetworkComponentState <T> > networkComponentStates = group.GetComponentDataArray <NetworkComponentState <T> >();
        EntityArray entities = group.GetEntityArray();

        ComponentDataFromEntity <NetworkSyncState> networkSyncStateEntities = EntityManager.GetComponentDataFromEntity <NetworkSyncState>();

        NetworkMemberInfo[] networkMemberInfos = reflectionUtility.GetNetworkMemberInfo(componentType);
        for (int i = 0; i < entities.Length; i++)
        {
            DynamicBuffer <int>    values = EntityManager.GetBuffer <NetworkValue>(networkComponentStates[i].dataEntity).Reinterpret <int>();
            ComponentDataContainer componentDataContainer = new ComponentDataContainer {
                ComponentTypeId = reflectionUtility.GetComponentTypeID(componentType),
            };
            for (int j = 0; j < networkMemberInfos.Length; j++)
            {
                NetworkMemberInfo <T> networkMemberInfo = (networkMemberInfos[j] as NetworkMemberInfo <T>);
                if (networkMemberInfo.netSyncOptions.InitOnly)
                {
                    continue;
                }

                int newValue = networkMemberInfo.GetValue(networkComponents[i], networkSyncStateEntities);
                if (newValue != values[j])
                {
                    componentDataContainer.MemberData.Add(new MemberDataContainer {
                        MemberId = j,
                        Data     = newValue,
                    });
                }
                values[j] = newValue;
            }

            if (componentDataContainer.MemberData.Count != 0)
            {
                NetworkSyncState networkSyncState = networkSyncStateComponents[i];
                ownNetworkSendMessageUtility.SetComponentData(entities[i], networkSyncState.actorId, networkSyncState.networkId, componentDataContainer);
                AllNetworkSendMessageUtility.SetComponentData(entities[i], networkSyncState.actorId, networkSyncState.networkId, componentDataContainer);
            }
        }
    }
Пример #6
0
    private void RemovedComponents <T>() where T : IComponentData
    {
        ComponentType  componentType = ComponentType.Create <T>();
        ComponentGroup group         = GetComponentGroup(ComponentType.Create <NetworkSyncState>(), ComponentType.Subtractive <T>(), ComponentType.Create <NetworkComponentState <T> >(), ComponentType.Create <NetworktAuthority>());
        ComponentDataArray <NetworkSyncState>           networkSyncStateComponents = group.GetComponentDataArray <NetworkSyncState>();
        ComponentDataArray <NetworkComponentState <T> > networkComponentStates     = group.GetComponentDataArray <NetworkComponentState <T> >();
        EntityArray entities = group.GetEntityArray();

        for (int i = 0; i < entities.Length; i++)
        {
            NetworkSyncState networkSyncState = networkSyncStateComponents[i];
            ownNetworkSendMessageUtility.RemoveComponent(entities[i], networkSyncState.actorId, networkSyncState.networkId, reflectionUtility.GetComponentTypeID(componentType));
            AllNetworkSendMessageUtility.RemoveComponent(entities[i], networkSyncState.actorId, networkSyncState.networkId, reflectionUtility.GetComponentTypeID(componentType));

            PostUpdateCommands.DestroyEntity(networkComponentStates[i].dataEntity);
            PostUpdateCommands.RemoveComponent <NetworkComponentState <T> >(entities[i]);
        }
    }
Пример #7
0
    private void AddedEntities()
    {
        EntityArray entities = addedSyncEntities.entities;
        ComponentDataArray <NetworkSync> networkSyncs = addedSyncEntities.networkSyncComponents;

        for (int i = 0; i < entities.Length; i++)
        {
            NetworkSync      networkSync = networkSyncs[i];
            int              instanceId  = networkSync.instanceId;
            NetworkSyncState component   = new NetworkSyncState()
            {
                actorId   = networkManager.LocalPlayerID,
                networkId = networkManager.GetNetworkId(),
            };
            Entity entity = entities[i];
            PostUpdateCommands.AddComponent(entity, component);

            if (NetworkUtility.CanAssignAuthority(networkManager, networkSync.authority))
            {
                PostUpdateCommands.AddComponent(entity, new NetworktAuthority());
            }

            NetworkEntityData networkEntityData = new NetworkEntityData {
                InstanceId = networkSync.instanceId,

                NetworkSyncEntity = new NetworkSyncEntity {
                    ActorId   = component.actorId,
                    NetworkId = component.networkId,
                }
            };

            for (int j = 0; j < AddComponentDataOnEntityAddedMethods.Count; j++)
            {
                if (AddComponentDataOnEntityAddedMethods[j].Invoke(this, ref entity, out ComponentDataContainer componentData))
                {
                    networkEntityData.ComponentData.Add(componentData);
                }
            }

            ownNetworkSendMessageUtility.AddEntity(networkEntityData);
            AllNetworkSendMessageUtility.AddEntity(networkEntityData);
        }
    }
    protected override void OnUpdate()
    {
        if (networkManager == null)
        {
            return;
        }

        for (int i = 0; i < addedNetworkEntities.Length; i++)
        {
            NetworkSyncState networkSyncState = addedNetworkEntities.networkSyncState[i];
            int hash = NetworkUtility.GetNetworkEntityHash(networkSyncState.actorId, networkSyncState.networkId);
            networkEntityMap.TryAdd(hash, addedNetworkEntities.entity[i]);
            PostUpdateCommands.AddComponent(addedNetworkEntities.entity[i], new NetworkEntityHash {
                hash = hash
            });
        }

        for (int i = 0; i < removedNetworkEntities.Length; i++)
        {
            int hash = removedNetworkEntities.networkEntityHash[i].hash;
            networkEntityMap.Remove(hash);
            PostUpdateCommands.RemoveComponent <NetworkEntityHash>(addedNetworkEntities.entity[i]);
        }


        //return;
        networkManager.Update();
        for (int i = 0; i < UpdateComponentsMethods.Count; i++)
        {
            UpdateComponentsMethods[i].Invoke(this);
        }
        for (int i = 0; i < gameObjectsToDestroy.Count; i++)
        {
            UnityEngine.Object.Destroy(gameObjectsToDestroy[i]);
        }
        gameObjectsToDestroy.Clear();
        networkFactory.FlushNetworkManager();
    }
    private void ReceiveNetworkUpdate(byte[] data)
    {
        NetworkSyncDataContainer networkSyncDataContainer = messageSerializer.Deserialize(data);

        if (LogReceivedMessages && (networkSyncDataContainer.AddedNetworkSyncEntities.Any() ||
                                    networkSyncDataContainer.RemovedNetworkSyncEntities.Any() ||
                                    networkSyncDataContainer.NetworkSyncDataEntities.Any()))
        {
            Debug.Log("ReceiveNetworkUpdate: " + NetworkMessageUtility.ToString(networkSyncDataContainer));
        }

        ComponentGroup group = GetComponentGroup(ComponentType.Create <NetworkSyncState>());
        ComponentDataArray <NetworkSyncState> networkSyncStateComponents = group.GetComponentDataArray <NetworkSyncState>();
        EntityArray entities = group.GetEntityArray();

        NativeHashMap <int, int> entityIndexMap = new NativeHashMap <int, int>(entities.Length, Allocator.Temp);

        for (int i = 0; i < entities.Length; i++)
        {
            NetworkSyncState networkSyncState = networkSyncStateComponents[i];
            int hash = GetHash(networkSyncState.actorId, networkSyncState.networkId);
            entityIndexMap.TryAdd(hash, i);
        }

        // added Entities
        List <NetworkEntityData> addedNetworkSyncEntities = networkSyncDataContainer.AddedNetworkSyncEntities;

        for (int i = 0; i < addedNetworkSyncEntities.Count; i++)
        {
            if (addedNetworkSyncEntities[i].NetworkSyncEntity.ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }

            Entity      entity      = reflectionUtility.GetEntityFactoryMethod(addedNetworkSyncEntities[i].InstanceId).Invoke(EntityManager);
            NetworkSync networkSync = EntityManager.GetComponentData <NetworkSync>(entity);
            if (NetworkUtility.CanAssignAuthority(networkManager, networkSync.authority))
            {
                PostUpdateCommands.AddComponent(entity, new NetworktAuthority());
            }

            PostUpdateCommands.AddComponent(entity, new NetworkSyncState {
                actorId   = addedNetworkSyncEntities[i].NetworkSyncEntity.ActorId,
                networkId = addedNetworkSyncEntities[i].NetworkSyncEntity.NetworkId,
            });

            var componentData = addedNetworkSyncEntities[i].ComponentData;
            for (int j = 0; j < componentData.Count; j++)
            {
                ComponentType componentType = reflectionUtility.GetComponentType(componentData[j].ComponentTypeId);
                AddComponentsMethods[componentType].Invoke(this, entity, componentData[j].MemberData);
            }

            if (addedNetworkSyncEntities[i].NetworkSyncEntity.ActorId != networkManager.LocalPlayerID)
            {
                NetworkSendSystem.AllNetworkSendMessageUtility.AddEntity(addedNetworkSyncEntities[i]);
            }
        }

        // removed Entities
        List <NetworkSyncEntity> removedNetworkSyncEntities = networkSyncDataContainer.RemovedNetworkSyncEntities;

        for (int i = 0; i < removedNetworkSyncEntities.Count; i++)
        {
            if (removedNetworkSyncEntities[i].ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }
            NetworkSyncEntity networkSyncEntity = removedNetworkSyncEntities[i];
            int hash = GetHash(networkSyncEntity.ActorId, networkSyncEntity.NetworkId);
            if (entityIndexMap.TryGetValue(hash, out int index))
            {
                Entity entity = entities[index];
                PostUpdateCommands.RemoveComponent <NetworkSyncState>(entity);
                PostUpdateCommands.DestroyEntity(entity);

                if (EntityManager.HasComponent <Transform>(entity))
                {
                    gameObjectsToDestroy.Add(EntityManager.GetComponentObject <Transform>(entity).gameObject);
                }
                for (int j = 0; j < RemoveComponentOnDestroyEntityMethods.Count; j++)
                {
                    RemoveComponentOnDestroyEntityMethods[j].Invoke(this, entity);
                }
            }

            if (removedNetworkSyncEntities[i].ActorId != networkManager.LocalPlayerID)
            {
                NetworkSendSystem.AllNetworkSendMessageUtility.RemoveEntity(removedNetworkSyncEntities[i]);
            }
        }

        // update components
        List <NetworkSyncDataEntityContainer> networkSyncDataEntities = networkSyncDataContainer.NetworkSyncDataEntities;

        for (int i = 0; i < networkSyncDataEntities.Count; i++)
        {
            NetworkSyncEntity networkSyncEntity = networkSyncDataEntities[i].NetworkSyncEntity;
            if (networkSyncEntity.ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }

            int hash = GetHash(networkSyncEntity.ActorId, networkSyncEntity.NetworkId);
            if (!entityIndexMap.TryGetValue(hash, out int index))
            {
                continue;
            }
            Entity entity = entities[index];

            List <ComponentDataContainer> addedComponents = networkSyncDataEntities[i].AddedComponents;
            List <int> removedComponents = networkSyncDataEntities[i].RemovedComponents;
            List <ComponentDataContainer> componentData = networkSyncDataEntities[i].ComponentData;

            for (int j = 0; j < addedComponents.Count; j++)
            {
                ComponentType componentType = reflectionUtility.GetComponentType(addedComponents[j].ComponentTypeId);
                AddComponentsMethods[componentType].Invoke(this, entity, addedComponents[j].MemberData);
            }

            for (int j = 0; j < componentData.Count; j++)
            {
                ComponentType componentType = reflectionUtility.GetComponentType(componentData[j].ComponentTypeId);
                SetComponentsMethods[componentType].Invoke(this, entity, componentData[j].MemberData);
            }

            for (int j = 0; j < removedComponents.Count; j++)
            {
                ComponentType componentType = reflectionUtility.GetComponentType(removedComponents[j]);
                RemoveComponentsMethods[componentType].Invoke(this, entity);
            }


            if (networkSyncEntity.ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }

            NetworkSendSystem.AllNetworkSendMessageUtility.AddComponents(entity, networkSyncEntity.ActorId, networkSyncEntity.NetworkId, addedComponents);
            NetworkSendSystem.AllNetworkSendMessageUtility.RemoveComponents(entity, networkSyncEntity.ActorId, networkSyncEntity.NetworkId, removedComponents);
            NetworkSendSystem.AllNetworkSendMessageUtility.SetComponentData(entity, networkSyncEntity.ActorId, networkSyncEntity.NetworkId, componentData);
        }

        entityIndexMap.Dispose();
    }