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);
        }
    }
Exemplo n.º 2
0
    public static string ToString(NetworkSyncDataContainer networkDataContainer)
    {
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.AppendLine("NetworkSyncDataEntityContainers: {");
        foreach (NetworkSyncDataEntityContainer networkSyncDataEntityContainer in networkDataContainer.NetworkSyncDataEntities)
        {
            stringBuilder.AppendLine(string.Format("{0}NetworkSyncEntity: ", new String(' ', tab1)) + "{");
            stringBuilder.AppendLine(string.Format("{0}NetworkId: {1}", new String(' ', tab2), networkSyncDataEntityContainer.NetworkSyncEntity.NetworkId));
            stringBuilder.AppendLine(string.Format("{0}ActorId: {1}", new String(' ', tab2), networkSyncDataEntityContainer.NetworkSyncEntity.ActorId));
            stringBuilder.AppendLine(string.Format("{0}", new String(' ', tab1)) + "}");

            stringBuilder.Append(string.Format("{0}AddedComponents: [ ", new String(' ', tab1)));
            if (networkSyncDataEntityContainer.AddedComponents.Any())
            {
                stringBuilder.AppendLine();
            }
            foreach (ComponentDataContainer componentDataContainer in networkSyncDataEntityContainer.AddedComponents)
            {
                stringBuilder.AppendLine(string.Format("{0}componentDataContainer: ", new String(' ', tab2)) + "{");
                stringBuilder.AppendLine(string.Format("{0}ComponentTypeId: {1}", new String(' ', tab3), componentDataContainer.ComponentTypeId));
                stringBuilder.AppendLine(string.Format("{0}MemberData: [ {1} ]", new String(' ', tab3), string.Join(", ", componentDataContainer.MemberData.Select(x => x.Data))));
                stringBuilder.AppendLine(string.Format("{0}", new String(' ', tab2)));
            }
            if (networkSyncDataEntityContainer.AddedComponents.Any())
            {
                stringBuilder.Append(new String(' ', tab1));
            }
            stringBuilder.AppendLine("]");

            stringBuilder.AppendLine(string.Format("{0}RemovedComponents: [ {1} ]", new String(' ', tab1), string.Join(", ", networkSyncDataEntityContainer.RemovedComponents)));

            stringBuilder.Append(string.Format("{0}ComponentData: [ ", new String(' ', tab1)));
            if (networkSyncDataEntityContainer.ComponentData.Any())
            {
                stringBuilder.AppendLine();
            }
            foreach (ComponentDataContainer componentDataContainer in networkSyncDataEntityContainer.ComponentData)
            {
                stringBuilder.AppendLine(string.Format("{0}ComponentDataContainer: ", new String(' ', tab2)) + "{");
                stringBuilder.AppendLine(string.Format("{0}ComponentTypeId: {1}", new String(' ', tab3), componentDataContainer.ComponentTypeId));
                stringBuilder.AppendLine(string.Format("{0}MemberData: [ {1} ]", new String(' ', tab3), string.Join(", ", componentDataContainer.MemberData.Select(x => x.Data))));
                stringBuilder.AppendLine(string.Format("{0}", new String(' ', tab2)) + "}");
            }
            if (networkSyncDataEntityContainer.ComponentData.Any())
            {
                stringBuilder.Append(new String(' ', tab1));
            }
            stringBuilder.AppendLine("]");
        }
        stringBuilder.AppendLine("}");
        stringBuilder.AppendLine();
        stringBuilder.AppendLine("AddedNetworkSyncEntities: {");
        foreach (NetworkEntityData networkEntityData in networkDataContainer.AddedNetworkSyncEntities)
        {
            stringBuilder.AppendLine(string.Format("{0}NetworkEntityData: ", new String(' ', tab1)) + "{");
            stringBuilder.AppendLine(string.Format("{0}NetworkSyncEntity: ", new String(' ', tab2)) + "{");
            stringBuilder.AppendLine(string.Format("{0}NetworkId: {1}", new String(' ', tab3), networkEntityData.NetworkSyncEntity.NetworkId));
            stringBuilder.AppendLine(string.Format("{0}ActorId: {1}", new String(' ', tab3), networkEntityData.NetworkSyncEntity.ActorId));
            stringBuilder.AppendLine(string.Format("{0}", new String(' ', tab2)) + "}");

            stringBuilder.Append(string.Format("{0}ComponentData: [ ", new String(' ', tab1)));
            if (networkEntityData.ComponentData.Any())
            {
                stringBuilder.AppendLine();
            }
            foreach (ComponentDataContainer componentDataContainer in networkEntityData.ComponentData)
            {
                stringBuilder.AppendLine(string.Format("{0}ComponentDataContainer: ", new String(' ', tab2)) + "{");
                stringBuilder.AppendLine(string.Format("{0}ComponentTypeId: {1}", new String(' ', tab3), componentDataContainer.ComponentTypeId));
                stringBuilder.AppendLine(string.Format("{0}MemberData: [ {1} ]", new String(' ', tab3), string.Join(", ", componentDataContainer.MemberData.Select(x => x.Data))));
                stringBuilder.AppendLine(string.Format("{0}", new String(' ', tab2)) + "}");
            }
            if (networkEntityData.ComponentData.Any())
            {
                stringBuilder.Append(new String(' ', tab1));
            }
            stringBuilder.AppendLine("]");
            stringBuilder.AppendLine(new String(' ', tab1) + "}");
        }
        stringBuilder.AppendLine("}");
        stringBuilder.AppendLine();
        stringBuilder.AppendLine("AddedNetworkSyncEntities: {");
        foreach (NetworkSyncEntity networkSyncEntity in networkDataContainer.RemovedNetworkSyncEntities)
        {
            stringBuilder.AppendLine(string.Format("{0}NetworkSyncEntity: ", new String(' ', tab1)) + "{");
            stringBuilder.AppendLine(string.Format("{0}NetworkId: {1}", new String(' ', tab2), networkSyncEntity.NetworkId));
            stringBuilder.AppendLine(string.Format("{0}ActorId: {1}", new String(' ', tab2), networkSyncEntity.ActorId));
            stringBuilder.AppendLine(string.Format("{0}", new String(' ', tab1)) + "}");
        }
        stringBuilder.AppendLine("}");

        return(stringBuilder.ToString());
    }