Ejemplo n.º 1
0
        protected internal override void Initialize(IntermediateSerializer serializer)
        {
            // If we have a base type then we need to deserialize it first.
            if (TargetType.BaseType != null)
            {
                _baseSerializer = serializer.GetTypeSerializer(TargetType.BaseType);
            }

            // Cache all our serializable properties.
            var properties = TargetType.GetProperties(_bindingFlags);

            foreach (var prop in properties)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, prop, out info))
                {
                    _elements.Add(info);
                }
            }

            // Cache all our serializable fields.
            var fields = TargetType.GetFields(_bindingFlags);

            foreach (var field in fields)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, field, out info))
                {
                    _elements.Add(info);
                }
            }
        }
Ejemplo n.º 2
0
        private int PropertyFieldLogic()
        {
            int retval = 0;

            EditorUtility.DisplayProgressBar("Generating " + ClassName, "Property Scanning Fields", 0);
            var fields = TargetType.GetFields(GetBindingFlags());

            for (int i = 0; i < fields.Length; i++)
            {
                if (helper.IsTypeHandled(fields[i].FieldType))
                {
                    var actualName    = fields[i].Name;
                    var upperCaseName = Char.ToUpperInvariant(actualName[0]) + actualName.Substring(1);
                    //add it to the enum
                    AddToEnum(upperCaseName);

                    //add it to the get
                    AddToGet(fields[i].FieldType, upperCaseName, actualName);

                    //add it to the set
                    AddToSet(fields[i].FieldType, upperCaseName, actualName);
                    retval++;
                }
            }

            return(retval);
        }
Ejemplo n.º 3
0
        protected internal override void Initialize(ContentTypeReaderManager manager)
        {
            base.Initialize(manager);

            Type baseType = TargetType.BaseType;

            if (baseType != null && baseType != typeof(object))
            {
                baseTypeReader = manager.GetTypeReader(baseType);
            }

            constructor = TargetType.GetDefaultConstructor();

            const BindingFlags attrs = (
                BindingFlags.NonPublic |
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.DeclaredOnly
                );

            /* Sometimes, overridden properties of abstract classes can show up even with
             * BindingFlags.DeclaredOnly is passed to GetProperties. Make sure that
             * all properties in this list are defined in this class by comparing
             * its get method with that of its base class. If they're the same
             * Then it's an overridden property.
             */
            PropertyInfo[] properties = TargetType.GetProperties(attrs);
            FieldInfo[]    fields     = TargetType.GetFields(attrs);
            readers = new List <ReadElement>(fields.Length + properties.Length);

            // Gather the properties.
            foreach (PropertyInfo property in properties)
            {
                MethodInfo pm = property.GetGetMethod(true);
                if (pm == null || pm != pm.GetBaseDefinition())
                {
                    continue;
                }

                ReadElement read = GetElementReader(manager, property);
                if (read != null)
                {
                    readers.Add(read);
                }
            }

            // Gather the fields.
            foreach (FieldInfo field in fields)
            {
                ReadElement read = GetElementReader(manager, field);
                if (read != null)
                {
                    readers.Add(read);
                }
            }
        }
Ejemplo n.º 4
0
        IEnumerable <IValidatedElement> IValidatedType.GetValidatedFields()
        {
            var flyweight = new MetadataValidatedElement(Ruleset);

            foreach (FieldInfo fieldInfo in TargetType.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                flyweight.UpdateFlyweight(fieldInfo);
                yield return(flyweight);
            }
        }
Ejemplo n.º 5
0
        /// <summary>Sets all attribute-tagged fields and properties in the given object from the config values provided.</summary>
        /// <remarks>If a value is not set in the config file, or is set to an invalid value, or is set out of range (where applicable), the default value specified in the attribute is used instead. All attribute-tagged fields and properties are therefore set to reasonable values after this method returns true.</remarks>
        /// <param name="targetObj">The object to configure. Must implement <see cref="IConfigurableAttr"/>.</param>
        /// <param name="config">The set of config values to apply.</param>
        /// <returns>Whether applying the configuration values succeeded.</returns>
        /// <exception cref="InvalidOperationException">If the field or property type is not compatible with the attribute used.</exception>
        /// <exception cref="NotImplementedException">If the attribute is one that is not yet supported.</exception>
        public static bool Configure(object targetObj, Dictionary <string, object> config)
        {
            Type?TargetType;
            IConfigurableAttr?TargetInst = null;  // Only valid for instances (non-static classes)

            if (targetObj is Type)
            {
                TargetType = (Type?)targetObj;
            }                                                         // For configuring static classes
            else
            {
                TargetType = targetObj?.GetType();
                if (targetObj is not IConfigurableAttr Target)
                {
                    Log.Warn("Tried to configure non-configurable object " + targetObj);
                    return(false);
                }
                TargetInst = Target;
            }
            if (TargetType == null)
            {
                Log.Error("Tried to configure object whose type cannot be determined."); return(false);
            }
            Log.Info("Reading config for " + TargetType.Name + '.');

            FieldInfo[]    Fields     = TargetType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
            PropertyInfo[] Properties = TargetType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

            foreach (FieldInfo Field in Fields)
            {
                Attribute?Attr = Attribute.GetCustomAttribute(Field, typeof(ConfigAttribute));
                if (Attr is null)
                {
                    continue;
                }                              // Field without attribute, ignore
                if (Attr is ConfigIntAttribute IntAttr)
                {
                    long Value = CheckInt(config, IntAttr);

                    if (Field.FieldType == typeof(int))
                    {
                        Field.SetValue(targetObj, (int)Value);
                    }
                    else if (Field.FieldType == typeof(uint))
                    {
                        Field.SetValue(targetObj, (uint)Value);
                    }
                    else if (Field.FieldType == typeof(short))
                    {
                        Field.SetValue(targetObj, (short)Value);
                    }
                    else if (Field.FieldType == typeof(ushort))
                    {
                        Field.SetValue(targetObj, (ushort)Value);
                    }
                    else if (Field.FieldType == typeof(byte))
                    {
                        Field.SetValue(targetObj, (byte)Value);
                    }
                    else if (Field.FieldType == typeof(sbyte))
                    {
                        Field.SetValue(targetObj, (sbyte)Value);
                    }
                    else if (Field.FieldType == typeof(long))
                    {
                        Field.SetValue(targetObj, (long)Value);
                    }
                    else if (Field.FieldType == typeof(ulong))
                    {
                        Field.SetValue(targetObj, (ulong)Value);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Field {IntAttr.Name} in {TargetType.FullName} used ConfigInt but is not a numeric type.");
                    }

                    config.Remove(IntAttr.Name);
                }
                else if (Attr is ConfigStringAttribute StrAttr)
                {
                    string Value = CheckString(config, StrAttr);

                    if (Field.FieldType == typeof(string))
                    {
                        Field.SetValue(targetObj, Value);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Field {StrAttr.Name} in {TargetType.FullName} used ConfigString but is not a string type.");
                    }

                    config.Remove(StrAttr.Name);
                }
                else if (Attr is ConfigBoolAttribute BoolAttr)
                {
                    bool Value = CheckBool(config, BoolAttr);

                    if (Field.FieldType == typeof(bool))
                    {
                        Field.SetValue(targetObj, Value);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Field {BoolAttr.Name} in {TargetType.FullName} used ConfigBool but is not a bool type.");
                    }

                    config.Remove(BoolAttr.Name);
                }
                else if (Attr is ConfigFloatAttribute FltAttr)
                {
                    float Value = CheckFloat(config, FltAttr);

                    if (Field.FieldType == typeof(float))
                    {
                        Field.SetValue(targetObj, Value);
                    }
                    else if (Field.FieldType == typeof(double))
                    {
                        Field.SetValue(targetObj, (double)Value);
                    }
                    else if (Field.FieldType == typeof(decimal))
                    {
                        Field.SetValue(targetObj, (decimal)Value);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Field {FltAttr.Name} in {TargetType.FullName} used ConfigFloat but is not a float type.");
                    }

                    config.Remove(FltAttr.Name);
                }
                else
                {
                    throw new NotImplementedException("Unsupported config type encountered: " + Attr?.GetType()?.FullName);
                }
            }

            foreach (PropertyInfo Prop in Properties)
            {
                Attribute?Attr = Attribute.GetCustomAttribute(Prop, typeof(ConfigAttribute));
                if (Attr is null)
                {
                    continue;
                }                               // Property without attribute, ignore
                if (Attr is ConfigIntAttribute IntAttr)
                {
                    long Value = CheckInt(config, IntAttr);

                    if (Prop.PropertyType == typeof(int))
                    {
                        Prop.SetValue(targetObj, (int)Value);
                    }
                    else if (Prop.PropertyType == typeof(uint))
                    {
                        Prop.SetValue(targetObj, (uint)Value);
                    }
                    else if (Prop.PropertyType == typeof(short))
                    {
                        Prop.SetValue(targetObj, (short)Value);
                    }
                    else if (Prop.PropertyType == typeof(ushort))
                    {
                        Prop.SetValue(targetObj, (ushort)Value);
                    }
                    else if (Prop.PropertyType == typeof(byte))
                    {
                        Prop.SetValue(targetObj, (byte)Value);
                    }
                    else if (Prop.PropertyType == typeof(sbyte))
                    {
                        Prop.SetValue(targetObj, (sbyte)Value);
                    }
                    else if (Prop.PropertyType == typeof(long))
                    {
                        Prop.SetValue(targetObj, (long)Value);
                    }
                    else if (Prop.PropertyType == typeof(ulong))
                    {
                        Prop.SetValue(targetObj, (ulong)Value);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Property {IntAttr.Name} in {TargetType.FullName} used ConfigInt but is not a numeric type.");
                    }

                    config.Remove(IntAttr.Name);
                }
                else if (Attr is ConfigStringAttribute StrAttr)
                {
                    string Value = CheckString(config, StrAttr);

                    if (Prop.PropertyType == typeof(string))
                    {
                        Prop.SetValue(targetObj, Value);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Property {StrAttr.Name} in {TargetType.FullName} used ConfigString but is not a string type.");
                    }

                    config.Remove(StrAttr.Name);
                }
                else if (Attr is ConfigBoolAttribute BoolAttr)
                {
                    bool Value = CheckBool(config, BoolAttr);

                    if (Prop.PropertyType == typeof(bool))
                    {
                        Prop.SetValue(targetObj, Value);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Field {BoolAttr.Name} in {TargetType.FullName} used ConfigBool but is not a bool type.");
                    }

                    config.Remove(BoolAttr.Name);
                }
                else if (Attr is ConfigFloatAttribute FltAttr)
                {
                    float Value = CheckFloat(config, FltAttr);

                    if (Prop.PropertyType == typeof(float))
                    {
                        Prop.SetValue(targetObj, Value);
                    }
                    else if (Prop.PropertyType == typeof(double))
                    {
                        Prop.SetValue(targetObj, (double)Value);
                    }
                    else if (Prop.PropertyType == typeof(decimal))
                    {
                        Prop.SetValue(targetObj, (decimal)Value);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Field {FltAttr.Name} in {TargetType.FullName} used ConfigFloat but is not a float type.");
                    }

                    config.Remove(FltAttr.Name);
                }
                else
                {
                    throw new NotImplementedException("Unsupported config type encountered: " + Attr.GetType().FullName);
                }
            }

            // Warn about any remaining items
            foreach (string Item in config.Keys)
            {
                if (Item == "Type" || Item == "Name")
                {
                    continue;
                }
                if (TargetInst is IOutput && (Item == "VisualizerName" || Item == "Modes"))
                {
                    continue;
                }
                Log.Warn($"Unknown config entry \"{Item}\" found while configuring {TargetType.FullName}.");
            }

            return(true);
        }