Пример #1
0
    private void SendData()
    {
        NetworkEventOptions networkEventOptions = new NetworkEventOptions {
            TargetActors = jonedPlayer.ToArray(),
            Receiver     = NetworkReceiverGroup.Target,
        };

        if (LogSendMessages)
        {
            Debug.Log("SendFullState:\n" + NetworkMessageUtility.ToString(networkSendMessageUtility.DataContainer));
        }
        networkManager.SendMessage(NetworkEvents.DataSync, messageSerializer.Serialize(networkSendMessageUtility.DataContainer), true, networkEventOptions);
        networkSendMessageUtility.Reset();
    }
Пример #2
0
    private void SendData()
    {
        NetworkEventOptions networkEventOptions = new NetworkEventOptions();

        byte[] data;
        if (networkManager.IsMaster)
        {
            if (!AllNetworkSendMessageUtility.DataContainer.AddedNetworkSyncEntities.Any() &&
                !AllNetworkSendMessageUtility.DataContainer.RemovedNetworkSyncEntities.Any() &&
                !AllNetworkSendMessageUtility.DataContainer.NetworkSyncDataEntities.Any())
            {
                return;
            }

            networkEventOptions.Receiver = NetworkReceiverGroup.Others;
            if (LogSendMessages)
            {
                LastSendMessage = NetworkMessageUtility.ToString(AllNetworkSendMessageUtility.DataContainer);
            }
            data = messageSerializer.Serialize(AllNetworkSendMessageUtility.DataContainer);
        }
        else
        {
            if (!ownNetworkSendMessageUtility.DataContainer.AddedNetworkSyncEntities.Any() &&
                !ownNetworkSendMessageUtility.DataContainer.RemovedNetworkSyncEntities.Any() &&
                !ownNetworkSendMessageUtility.DataContainer.NetworkSyncDataEntities.Any())
            {
                return;
            }

            networkEventOptions.Receiver = NetworkReceiverGroup.MasterClient;
            if (LogSendMessages)
            {
                LastSendMessage = NetworkMessageUtility.ToString(ownNetworkSendMessageUtility.DataContainer);
            }
            data = messageSerializer.Serialize(ownNetworkSendMessageUtility.DataContainer);
        }
        //Debug.Log("NetworkSendSystem:\n" + LastSendMessage);
        networkManager.SendMessage(NetworkEvents.DataSync, data, true, networkEventOptions);

        ownNetworkSendMessageUtility.Reset();
        AllNetworkSendMessageUtility.Reset();
    }
    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));
        }


        // 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 = NetworkUtility.GetNetworkEntityHash(networkSyncEntity.ActorId, networkSyncEntity.NetworkId);
            if (networkEntityMap.TryGetValue(hash, out Entity entity))
            {
                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 = NetworkUtility.GetNetworkEntityHash(networkSyncEntity.ActorId, networkSyncEntity.NetworkId);
            if (!networkEntityMap.TryGetValue(hash, out Entity entity))
            {
                continue;
            }

            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);
        }
    }
Пример #4
0
    void ReceiveNetworkUpdate(byte[] data)
    {
        var container = messageSerializer.Deserialize(data);

        if (LogReceivedMessages && (container.Added.Any() ||
                                    container.Removed.Any() ||
                                    container.Entities.Any()))
        {
            Debug.Log("ReceiveNetworkUpdate: " + NetworkMessageUtility.ToString(container));
        }
        var group      = GetComponentGroup(ComponentType.Create <SyncState>());
        var components = group.GetComponentDataArray <SyncState>();
        var entities   = group.GetEntityArray();
        var map        = new NativeHashMap <int, int>(entities.Length, Allocator.Temp);

        for (int i = 0; i < entities.Length; i++)
        {
            int hash = GetHash(components[i].actorId, components[i].networkId);
            map.TryAdd(hash, i);
        }

        var addedEntities = container.Added;

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

            var addedEntity = reflectionUtility
                              .GetSpawn(addedEntities[i].InstanceId)
                              .Invoke(EntityManager);

            var state = new SyncState {
                actorId   = addedEntities[i].Id.ActorId,
                networkId = addedEntities[i].Id.NetworkId,
            };
            PostUpdateCommands.AddComponent(addedEntity, state);

            var componentData = addedEntities[i].Components;
            for (int j = 0; j < componentData.Count; j++)
            {
                var componentType = reflectionUtility
                                    .GetType(componentData[j].TypeId);
                AddComponentsMethods[componentType]
                .Invoke(this, addedEntity, componentData[j].Fields);
            }
            if (addedEntities[i].Id.ActorId != networkManager.LocalPlayerID)
            {
                NetworkSendSystem.AllNetworkSendMessageUtility.AddEntity(addedEntities[i]);
            }
        }

        // removed Entities
        var removedEntities = container.Removed;

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

            int hash = GetHash(removedEntities[i].ActorId, removedEntities[i].NetworkId);
            if (map.TryGetValue(hash, out int index))
            {
                var entity = entities[index];
                PostUpdateCommands.RemoveComponent <SyncState>(entity);
                PostUpdateCommands.DestroyEntity(entity);

                if (EntityManager.HasComponent <Transform>(entity))
                {
                    var gameObject = EntityManager.GetComponentObject <Transform>(entity).gameObject;
                    gameObjectsToDestroy.Add(gameObject);
                }
                for (int j = 0; j < RemoveComponentOnDestroyEntityMethods.Count; j++)
                {
                    RemoveComponentOnDestroyEntityMethods[j].Invoke(this, entity);
                }
            }
            if (removedEntities[i].ActorId != networkManager.LocalPlayerID)
            {
                NetworkSendSystem.AllNetworkSendMessageUtility.RemoveEntity(removedEntities[i]);
            }
        }

        // update components
        var updateEntities = container.Entities;

        for (int i = 0; i < updateEntities.Count; i++)
        {
            var updateEntity = updateEntities[i].Id;
            if (updateEntity.ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }

            int hash = GetHash(updateEntity.ActorId, updateEntity.NetworkId);
            if (!map.TryGetValue(hash, out int index))
            {
                continue;
            }

            var entity            = entities[index];
            var addedComponents   = updateEntities[i].AddedComponents;
            var removedComponents = updateEntities[i].RemovedComponents;
            var componentData     = updateEntities[i].Components;
            for (int j = 0; j < addedComponents.Count; j++)
            {
                var componentType = reflectionUtility.GetType(addedComponents[j].TypeId);
                AddComponentsMethods[componentType].Invoke(this, entity, addedComponents[j].Fields);
            }
            for (int j = 0; j < componentData.Count; j++)
            {
                var componentType = reflectionUtility.GetType(componentData[j].TypeId);
                SetComponentsMethods[componentType].Invoke(this, entity, componentData[j].Fields);
            }
            for (int j = 0; j < removedComponents.Count; j++)
            {
                var componentType = reflectionUtility.GetType(removedComponents[j]);
                RemoveComponentsMethods[componentType].Invoke(this, entity);
            }
            if (updateEntity.ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }

            NetworkSendSystem.AllNetworkSendMessageUtility.AddComponents(
                entity, updateEntity.ActorId, updateEntity.NetworkId, addedComponents);
            NetworkSendSystem.AllNetworkSendMessageUtility.RemoveComponents(
                entity, updateEntity.ActorId, updateEntity.NetworkId, removedComponents);
            NetworkSendSystem.AllNetworkSendMessageUtility.SetComponent(
                entity, updateEntity.ActorId, updateEntity.NetworkId, componentData);
        }
        map.Dispose();
    }