Beispiel #1
0
        public static bool IsEntityFieldSerializable(FieldInfo fieldInfo)
        {
            if (fieldInfo == null)
            {
                return(false);
            }

            Entity.FieldSerializeAttribute attr = fieldInfo.GetCustomAttribute <Entity.FieldSerializeAttribute>(true);
            if (attr == null)
            {
                return(false);
            }

            bool typeSupported = EntitySystemWorld.Instance.IsEntityFieldSerializable(attr.SupportedSerializationTypes);

            return(typeSupported);
        }
Beispiel #2
0
        /// <summary>
        /// 获得字段上的FieldSerializeAttribute标记
        /// </summary>
        /// <param name="entityOrEntityType">true, Entity; false, EntityType</param>
        /// <param name="fieldInfo">字段</param>
        /// <returns></returns>
        private static string GetFieldSerializeName(bool entityOrEntityType, FieldInfo fieldInfo)
        {
            string result = fieldInfo.Name;

            if (entityOrEntityType)
            {
                Entity.FieldSerializeAttribute attr = fieldInfo.GetCustomAttribute <Entity.FieldSerializeAttribute>(true);
                if (attr != null && !string.IsNullOrEmpty(attr.PropertyName))
                {
                    result = attr.PropertyName;
                }
            }
            else
            {
                EntityType.FieldSerializeAttribute attr = fieldInfo.GetCustomAttribute <EntityType.FieldSerializeAttribute>(true);
                if (attr != null && !string.IsNullOrEmpty(attr.PropertyName))
                {
                    result = attr.PropertyName;
                }
            }
            return(result);
        }
Beispiel #3
0
        private ClassInfo addClassInfo(Type type)
        {
            if (type == null)
            {
                return(null);
            }

            ClassInfo classInfo = GetClassInfoByEntityClassType(type);

            if (classInfo != null)
            {
                return(classInfo);
            }

            string typeName       = type.FullName + "Type";
            Type   typeEntityType = EntitySystemWorld.Instance.FindEntityClassType(typeName);

            if (typeEntityType == null)
            {
                Log.Fatal(string.Format("Type class not defined for \"{0}\"", type.FullName));
                return(null);
            }

            if (!typeof(EntityType).IsAssignableFrom(typeEntityType))
            {
                Log.Fatal(string.Format("Type class not inherited by EntityType \"{0}\"", typeEntityType.FullName));
                return(null);
            }

            Type      baseType      = type.BaseType;
            ClassInfo baseClassInfo = null;

            if (baseType != null && typeof(Entity).IsAssignableFrom(baseType))
            {
                baseClassInfo = addClassInfo(baseType);
            }

            classInfo = new ClassInfo();
            classInfo.baseClassInfo   = baseClassInfo;
            classInfo.typeClassType   = typeEntityType;
            classInfo.entityClassType = type;

            #region EntityType 序列化字段
            classInfo.entityTypeSerializableFieldItemList = new List <ClassInfo.EntityTypeSerializableFieldItem>();
            classInfo.entityTypeSerializableFields        = new ReadOnlyCollection <ClassInfo.EntityTypeSerializableFieldItem>(classInfo.entityTypeSerializableFieldItemList);
            FieldInfo[] entityTypeFields = classInfo.typeClassType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            for (int i = 0; i < entityTypeFields.Length; i++)
            {
                FieldInfo fieldInfo = entityTypeFields[i];
                if (fieldInfo.DeclaringType != typeEntityType)
                {
                    continue;
                }

                EntityType.FieldSerializeAttribute   fieldSerializeAttribute = null;
                EntityType.FieldSerializeAttribute[] rAttrs = (EntityType.FieldSerializeAttribute[])fieldInfo.GetCustomAttributes(typeof(EntityType.FieldSerializeAttribute), true);
                if (rAttrs.Length > 0)
                {
                    fieldSerializeAttribute = rAttrs[0];
                }

                if (fieldSerializeAttribute == null)
                {
                    continue;
                }

                ClassInfo.EntityTypeSerializableFieldItem entityTypeSerializableFieldItem = new ClassInfo.EntityTypeSerializableFieldItem();
                entityTypeSerializableFieldItem.field = fieldInfo;
                classInfo.entityTypeSerializableFieldItemList.Add(entityTypeSerializableFieldItem);
            }
            #endregion

            #region Entity 序列化字段
            classInfo.entitySerializableFieldItemList = new List <ClassInfo.EntitySerializableFieldItem>();
            classInfo.entitySerializableFields        = new ReadOnlyCollection <EntityTypes.ClassInfo.EntitySerializableFieldItem>(classInfo.entitySerializableFieldItemList);
            FieldInfo[] entityFields = classInfo.entityClassType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            for (int j = 0; j < entityFields.Length; j++)
            {
                FieldInfo fieldInfo = entityFields[j];
                if (fieldInfo.DeclaringType == typeEntityType)
                {
                    continue;
                }

                Entity.FieldSerializeAttribute   fieldSerializeAttribute = null;
                Entity.FieldSerializeAttribute[] rAttrs = (Entity.FieldSerializeAttribute[])fieldInfo.GetCustomAttributes(typeof(Entity.FieldSerializeAttribute), true);
                if (rAttrs.Length > 0)
                {
                    fieldSerializeAttribute = rAttrs[0];
                }

                if (fieldSerializeAttribute == null)
                {
                    continue;
                }

                ClassInfo.EntitySerializableFieldItem entitySerializableFieldItem = new ClassInfo.EntitySerializableFieldItem();
                entitySerializableFieldItem.field = fieldInfo;
                classInfo.entitySerializableFieldItemList.Add(entitySerializableFieldItem);
            }
            #endregion

            #region _type字段
            FieldInfo[] typeFields = classInfo.entityClassType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            for (int k = 0; k < typeFields.Length; k++)
            {
                FieldInfo fieldInfo = typeFields[k];
                object[]  attrs     = fieldInfo.GetCustomAttributes(typeof(Entity.TypeFieldAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }
                classInfo.fieldInfo = fieldInfo;
                break;
            }

            if (classInfo.fieldInfo == null)
            {
                classInfo.fieldInfo = classInfo.entityClassType.GetField("_type", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            }

            if (classInfo.fieldInfo == null && !typeof(LogicComponent).IsAssignableFrom(type))
            {
                Log.Fatal("Field \"_type\" not defined for \"{0}\"", type.FullName);
                return(null);
            }
            #endregion

            #region 网络处理函数
            MethodInfo[] methods = classInfo.entityClassType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            for (int l = 0; l < methods.Length; l++)
            {
                MethodInfo methodInfo = methods[l];
                Entity.NetworkReceiveAttribute[] array7 = (Entity.NetworkReceiveAttribute[])methodInfo.GetCustomAttributes(typeof(Entity.NetworkReceiveAttribute), false);
                bool flag15 = array7.Length != 0;
                if (flag15)
                {
                    Entity.NetworkReceiveAttribute networkReceiveAttribute = array7[0];
                    Entity.NetworkDirections       direction = networkReceiveAttribute.Direction;
                    ushort messageIdentifier = networkReceiveAttribute.MessageIdentifier;
                    while (classInfo.networkSynchronizedMetaBuffer.Count <= (int)messageIdentifier)
                    {
                        classInfo.networkSynchronizedMetaBuffer.Add(null);
                    }
                    bool flag16 = classInfo.networkSynchronizedMetaBuffer[(int)messageIdentifier] != null;
                    if (flag16)
                    {
                        Log.Fatal("EntitySystem: NetworkSynchronizedAttribute is already defined for network message \"{0}\" for entity class \"{0}\". Method name is a \"{2}\"", messageIdentifier, classInfo.entityClassType.Name, methodInfo.Name);
                        return(null);
                    }
                    classInfo.networkSynchronizedMetaBuffer[(int)messageIdentifier] = new EntityTypes.ClassInfo.NetworkSynchronizedMeta(direction, methodInfo);
                }
            }
            #endregion

            classInfoNetworkUIN += 1u;
            classInfo.networkUIN = classInfoNetworkUIN;
            classes.Add(classInfo);
            typeClassInfoDic.Add(type, classInfo);
            typeNameClassInfoDic.Add(type.Name, classInfo);
            return(classInfo);
        }