Example #1
0
        public static GhostFieldAttribute GetGhostFieldAttribute(Mono.Cecil.TypeReference parentType, Mono.Cecil.IMemberDefinition componentField)
        {
            if (parentType != null && GhostAuthoringModifiers.GhostDefaultOverrides.TryGetValue(parentType.FullName.Replace('/', '+'), out var newComponent))
            {
                foreach (var field in newComponent.fields)
                {
                    if (field.name == componentField.Name)
                    {
                        return(field.attribute);
                    }
                }
                return(default(GhostFieldAttribute));
            }

            var attribute = componentField.GetAttribute <GhostFieldAttribute>();

            if (attribute != null)
            {
                var fieldAttribute = new GhostFieldAttribute();
                if (attribute.HasProperties)
                {
                    foreach (var a in attribute.Properties)
                    {
                        typeof(GhostFieldAttribute).GetProperty(a.Name)?.SetValue(fieldAttribute, a.Argument.Value);
                    }
                }

                return(fieldAttribute);
            }
            return(default(GhostFieldAttribute));
        }
Example #2
0
 void ParseAttribute(GhostFieldAttribute attribute)
 {
     if (attribute != null)
     {
         if (attribute.Quantization >= 0)
         {
             Attribute.quantization = attribute.Quantization;
         }
         if ((int)attribute.Smoothing >= 0)
         {
             Attribute.smoothing = (uint)attribute.Smoothing;
         }
         if (attribute.SubType > 0)
         {
             Attribute.subtype = attribute.SubType;
         }
         if (attribute.Composite)
         {
             Attribute.composite = attribute.Composite;
         }
         if (attribute.MaxSmoothingDistance > 0)
         {
             Attribute.maxSmoothingDist = attribute.MaxSmoothingDistance;
         }
     }
 }
Example #3
0
        static void AddToComponentList(List <GhostComponent> newComponents,
                                       World tempWorld, Entity convertedEntity, int entityIndex)
        {
            var compTypes = tempWorld.EntityManager.GetComponentTypes(convertedEntity);

            compTypes.Sort(default(ComponentNameComparer));



            for (int i = 0; i < compTypes.Length; ++i)
            {
                var managedType = compTypes[i].GetManagedType();
                if (managedType == typeof(Prefab) || managedType == typeof(LinkedEntityGroup))
                {
                    continue;
                }

                bool hasDefaultComponent = GhostDefaultOverrides.TryGetValue(managedType.FullName, out var defaultComponent);
                var  fields = new List <GhostComponentField>();
                foreach (var componentField in managedType.GetFields(BindingFlags.Instance | BindingFlags.Public))
                {
                    GhostFieldAttribute attr = null;
                    if (hasDefaultComponent)
                    {
                        attr = defaultComponent.fields.FirstOrDefault(f => f.name == componentField.Name).attribute;
                    }

                    if (attr == null)
                    {
                        var attributes = componentField.GetCustomAttributes <GhostFieldAttribute>().ToArray();
                        if (attributes.Length > 0)
                        {
                            attr = attributes[0];
                        }
                    }

                    if (attr == null || !attr.SendData)
                    {
                        continue;
                    }

                    FillSubFields(componentField, attr, fields);
                }

                var newComponent = new GhostComponent
                {
                    name        = managedType.FullName,
                    attribute   = default,
Example #4
0
        static void FillSubFields(FieldInfo field, GhostFieldAttribute attr, List <GhostFieldModifier> fieldsList, string parentPrefix = "")
        {
            var typeAttribute = new TypeAttribute
            {
                composite        = attr.Composite,
                smoothing        = (uint)attr.Smoothing,
                quantization     = attr.Quantization,
                maxSmoothingDist = attr.MaxSmoothingDistance,
                subtype          = attr.SubType
            };

            if (!field.FieldType.IsValueType)
            {
                return;
            }

            if (field.FieldType.IsPrimitive || field.FieldType.IsEnum)
            {
                if (CodeGenTypes.Registry.CanGenerateType(new TypeDescription(field.FieldType, typeAttribute)))
                {
                    fieldsList.Add(new GhostFieldModifier
                    {
                        name      = parentPrefix + field.Name,
                        attribute = attr
                    });
                }
                return;
            }
            if (CodeGenTypes.Registry.CanGenerateType(new TypeDescription(field.FieldType, typeAttribute)))
            {
                fieldsList.Add(new GhostFieldModifier
                {
                    name      = parentPrefix + field.Name,
                    attribute = attr
                });
                return;
            }
            foreach (var f in field.FieldType.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                var attributes = f.GetCustomAttributes <GhostFieldAttribute>().ToArray();
                if (attributes.Length > 0 && !attributes[0].SendData)
                {
                    continue;
                }
                FillSubFields(f, attr, fieldsList, $"{parentPrefix+field.Name}.");
            }
        }
Example #5
0
        public static GhostFieldAttribute GetGhostFieldAttribute(Mono.Cecil.TypeReference parentType,
                                                                 Mono.Cecil.FieldDefinition componentField)
        {
            var attribute = componentField.GetAttribute <GhostFieldAttribute>();

            if (attribute != null)
            {
                var fieldAttribute = new GhostFieldAttribute();
                if (attribute.HasProperties)
                {
                    foreach (var a in attribute.Properties)
                    {
                        typeof(GhostFieldAttribute).GetProperty(a.Name)?.SetValue(fieldAttribute, a.Argument.Value);
                    }
                }

                return(fieldAttribute);
            }

            return(default(GhostFieldAttribute));
        }
        static private TypeInformation ParseTypeField(Mono.Cecil.IMemberDefinition fieldInfo, Mono.Cecil.TypeReference fieldType,
                                                      GhostFieldAttribute ghostField, TypeAttribute inheritedAttribute, TypeAttribute.AttributeFlags inheriteAttributedMask, string parent = "")
        {
            var information = new TypeInformation(fieldInfo, fieldType, ghostField, inheritedAttribute, inheriteAttributedMask, parent);

            //blittable also contains bool, but does not contains enums
            if (fieldType.IsBlittable())
            {
                return(information);
            }

            var fieldDef = fieldType.Resolve();

            if (fieldDef.IsEnum)
            {
                return(information);
            }

            if (!fieldDef.IsStruct())
            {
                return(default);
 void ParseAttribute(GhostFieldAttribute attribute)
 {
     if (attribute != null)
     {
         if (attribute.Quantization >= 0)
         {
             Attribute.quantization = attribute.Quantization;
         }
         if (attribute.Interpolate)
         {
             Attribute.interpolate = attribute.Interpolate;
         }
         if (attribute.SubType > 0)
         {
             Attribute.subtype = attribute.SubType;
         }
         if (attribute.Composite)
         {
             Attribute.composite = attribute.Composite;
         }
     }
 }
Example #8
0
        public TypeInformation(Mono.Cecil.IMemberDefinition field, Mono.Cecil.TypeReference fieldType, GhostFieldAttribute ghostField,
                               TypeAttribute inheritedAttribute, TypeAttribute.AttributeFlags inheritedAttributeMask, string parent = null)
        {
            Type          = fieldType;
            FieldName     = field.Name;
            DeclaringType = field.DeclaringType;
            Fields        = new List <TypeInformation>();
            Attribute     = inheritedAttribute;
            AttributeMask = inheritedAttributeMask;
            //Always reset the subtype. It cannot be inherithed like this
            Attribute.subtype = 0;
            //Reset flags based on inheritance mask
            Attribute.composite &= (AttributeMask & TypeAttribute.AttributeFlags.Composite) != 0;

            Attribute.smoothing &= inheritedAttribute.smoothing;

            if ((AttributeMask & TypeAttribute.AttributeFlags.Quantized) == 0)
            {
                Attribute.quantization = -1;
            }


            ParseAttribute(ghostField);
            Parent = string.IsNullOrEmpty(parent) ? "" : parent;
        }
Example #9
0
        public GhostComponentGenType(TypeDefinition typeDefinition, CodeGenService.GhostComponent config = null) : base(
                typeDefinition, config)
        {
            templateFilePath = CodeGenServiceEditor.GhostComponentSerializerPath;

            GhostComponentAttribute ghostComponentAttribute = null;

            if (config != null)
            {
                ghostComponentAttribute = config.Attribute;
            }
            else
            {
                ghostComponentAttribute = CecilExtensions.GetGhostComponentAttribute(typeDefinition);
            }

            if (ghostComponentAttribute == null)
            {
                Debug.LogError(typeDefinition.FullName);
            }

            templateArgs.IsUpdateValue = ghostComponentAttribute.IsUpdateValue ? "true" : "false";
            templateArgs.SendType      = ghostComponentAttribute.SendType.ToString();
            templateArgs.Fields        = new List <FieldT4Info>();

            foreach (var fieldDefinition in typeDefinition.Fields)
            {
                if (!fieldDefinition.FieldType.IsValueType || fieldDefinition.Name.StartsWith("<"))
                {
                    continue;
                }

                bool isGhostField = fieldDefinition.IsGhostField();

                CodeGenService.GhostComponentField field = null;
                if (config != null)
                {
                    field        = config.Fields.FirstOrDefault(f => f.Name == fieldDefinition.Name);
                    isGhostField = field != null;
                }

                if (!isGhostField)
                {
                    continue;
                }

                GhostFieldAttribute attr = null;
                if (field != null)
                {
                    attr = field.Attribute;
                }
                else
                {
                    attr = CecilExtensions.GetGhostFieldAttribute(typeDefinition, fieldDefinition);
                }

                TypeReference fieldType = fieldDefinition.FieldType;

                string type = ConvertBaseValueType(fieldType.Name);
                templateArgs.Fields.Add(new FieldT4Info
                {
                    Name                 = fieldDefinition.Name,
                    TypeName             = type,
                    TypeName1            = ToUpper(type),
                    Interpolate          = attr.Interpolate,
                    SendData             = attr.SendData,
                    InterpolateMethodStr = GetInterpolateMethodStr(type)
                });
            }
        }