Example #1
0
        private void FindSerializers(Entity entity)
        {
            // Add entity data handlers
            if (showInfo.IntValue > 0)
            {
                GameDebug.Log("  FindSerializers");
            }
            var componentTypes = entityManager.GetComponentTypes(entity);

            // Sort to ensure order when serializing components
            var typeArray = componentTypes.ToArray();

            Array.Sort(typeArray, (type1, type2) =>
                       string.Compare(type1.GetManagedType().Name, type2.GetManagedType().Name, StringComparison.Ordinal));

            var serializedComponentType   = typeof(IReplicatedState);
            var predictedComponentType    = typeof(IPredictedStateBase);
            var interpolatedComponentType = typeof(IInterpolatedStateBase);

            foreach (var componentType in typeArray)
            {
                var managedType = componentType.GetManagedType();

                if (!typeof(IComponentData).IsAssignableFrom(managedType))
                {
                    continue;
                }

                if (serializedComponentType.IsAssignableFrom(managedType))
                {
                    if (showInfo.IntValue > 0)
                    {
                        GameDebug.Log("   new SerializedComponentDataHandler for:" + managedType.Name);
                    }

                    var serializer = serializers.CreateNetSerializer(managedType, entityManager, entity, this);
                    if (serializer != null)
                    {
                        netSerializables.Add(serializer);
                    }
                }
                else if (predictedComponentType.IsAssignableFrom(managedType))
                {
                    var interfaceTypes = managedType.GetInterfaces();
                    foreach (var it in interfaceTypes)
                    {
                        if (it.IsGenericType)
                        {
                            var type = it.GenericTypeArguments[0];
                            if (showInfo.IntValue > 0)
                            {
                                GameDebug.Log("   new IPredictedDataHandler for:" + it.Name + " arg type:" + type);
                            }

                            var serializer =
                                serializers.CreatePredictedSerializer(managedType, entityManager, entity, this);
                            if (serializer != null)
                            {
                                netPredicted.Add(serializer);
                            }

                            break;
                        }
                    }
                }
                else if (interpolatedComponentType.IsAssignableFrom(managedType))
                {
                    var interfaceTypes = managedType.GetInterfaces();
                    foreach (var it in interfaceTypes)
                    {
                        if (it.IsGenericType)
                        {
                            var type = it.GenericTypeArguments[0];
                            if (showInfo.IntValue > 0)
                            {
                                GameDebug.Log("   new IInterpolatedDataHandler for:" + it.Name + " arg type:" + type);
                            }

                            var serializer =
                                serializers.CreateInterpolatedSerializer(managedType, entityManager, entity, this);
                            if (serializer != null)
                            {
                                netInterpolated.Add(serializer);
                            }

                            break;
                        }
                    }
                }
            }
        }
Example #2
0
        public void Register(int entityId, Entity entity)
        {
            if (showInfo.IntValue > 0)
            {
                if (entityManager.HasComponent <Transform>(entity))
                {
                    GameDebug.Log("RepEntity REGISTER NetID:" + entityId + " Entity:" + entity + " GameObject:" +
                                  entityManager.GetComponentObject <Transform>(entity).name);
                }
                else
                {
                    GameDebug.Log("RepEntity REGISTER NetID:" + entityId + " Entity:" + entity);
                }
            }


            // Grow to make sure there is room for entity
            if (entityId >= replicatedData.Count)
            {
                var count     = entityId - replicatedData.Count + 1;
                var emptyData = new ReplicatedData();
                for (var i = 0; i < count; i++)
                {
                    replicatedData.Add(emptyData);
                }
            }

            GameDebug.Assert(replicatedData[entityId].Entity == Entity.Null, "ReplicatedData has entity set:{0}",
                             replicatedData[entityId].Entity);

            netSerializables.Clear();
            netPredicted.Clear();
            netInterpolated.Clear();

            //var go = entityManager.HasComponent<Transform>(entity)
            //    ? entityManager.GetComponentObject<Transform>(entity).gameObject
            //    : null;


            FindSerializers(entity);

            if (entityManager.HasComponent <EntityGroupChildren>(entity))
            {
                var buffer = entityManager.GetBuffer <EntityGroupChildren>(entity);
                for (var i = 0; i < buffer.Length; i++)
                {
                    var childEntity = buffer[i].Entity;
                    if (showInfo.IntValue > 0)
                    {
                        GameDebug.Log(" ReplicatedEntityChildren: " + i + " = " + childEntity);
                    }
                    FindSerializers(childEntity);
                }
            }

            var data = new ReplicatedData
            {
                Entity = entity,
                //      gameObject = go,
                SerializableArray = netSerializables.ToArray(),
                PredictedArray    = netPredicted.ToArray(),
                InterpolatedArray = netInterpolated.ToArray()
            };

            replicatedData[entityId] = data;
        }
Example #3
0
        private void CreateSerializerFactories()
        {
            var componentDataType = typeof(IComponentData);
            var serializedType    = typeof(IReplicatedState);
            var predictedType     = typeof(IPredictedStateBase);
            var interpolatedType  = typeof(IInterpolatedStateBase);

            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (!componentDataType.IsAssignableFrom(type))
                    {
                        continue;
                    }

                    if (serializedType.IsAssignableFrom(type))
                    {
                        //                    GameDebug.Log("Making serializer factory for type:" + type);

                        var method = type.GetMethod("CreateSerializerFactory");
                        if (method == null)
                        {
                            GameDebug.LogError("Replicated component " + type + " has no CreateSerializerFactory");
                            continue;
                        }

                        if (method.ReturnType != typeof(IReplicatedStateSerializerFactory))
                        {
                            GameDebug.LogError("Replicated component " + type +
                                               " CreateSerializerFactory does not have return type IReplicatedComponentSerializerFactory");
                            continue;
                        }

                        var result = method.Invoke(null, new object[] { });
                        netSerializerFactories.Add(type, (IReplicatedStateSerializerFactory)result);
                    }

                    if (predictedType.IsAssignableFrom(type))
                    {
                        //                    GameDebug.Log("Making predicted serializer factory for type:" + type);

                        var method = type.GetMethod("CreateSerializerFactory");
                        if (method == null)
                        {
                            GameDebug.LogError("Predicted component " + type + " has no CreateSerializerFactory");
                            continue;
                        }

                        if (method.ReturnType != typeof(IPredictedStateSerializerFactory))
                        {
                            GameDebug.LogError("Replicated component " + type +
                                               " CreateSerializerFactory does not have return type IPredictedComponentSerializerFactory");
                            continue;
                        }

                        var result = method.Invoke(null, new object[] { });
                        predictedSerializerFactories.Add(type, (IPredictedStateSerializerFactory)result);
                    }

                    if (interpolatedType.IsAssignableFrom(type))
                    {
                        //                    GameDebug.Log("Making interpolated serializer factory for type:" + type);

                        var method = type.GetMethod("CreateSerializerFactory");
                        if (method == null)
                        {
                            GameDebug.LogError("Interpolated component " + type + " has no CreateSerializerFactory");
                            continue;
                        }

                        if (method.ReturnType != typeof(IInterpolatedStateSerializerFactory))
                        {
                            GameDebug.LogError("Replicated component " + type +
                                               " CreateSerializerFactory does not have return type IInterpolatedComponentSerializerFactory");
                            continue;
                        }


                        var result = method.Invoke(null, new object[] { });
                        interpolatedSerializerFactories.Add(type, (IInterpolatedStateSerializerFactory)result);
                    }
                }
            }
        }