Esempio n. 1
0
        public StringControl(PropertyInfo prop, IAfterglowPlugin plugin) : base(prop)
        {
            ConfigStringAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigStringAttribute)) as ConfigStringAttribute;

            if (configAttribute == null)
            {
                throw new Exception("prop is not a ConfigStringAttribute");
            }

            //Create value Text Box
            _valueTextBox           = new TextBox();
            _valueTextBox.Name      = Guid.NewGuid().ToString();
            _valueTextBox.MaxLength = configAttribute.MaxLength;
            _valueTextBox.DataBindings.Add("Text", plugin, prop.Name, false, DataSourceUpdateMode.OnPropertyChanged, null);
            this.Controls.Add(_valueTextBox);

            InitializeComponent();
        }
Esempio n. 2
0
        /// <summary>
        /// Loads settings from storage
        /// </summary>
        public T LoadFromStorage <T>(string propertyName)
        {
            T result = default(T);

            if (Table == null)
            {
                //TODO Logger.Error("Storage has not be setup for " + this.Name);
                return(result);
            }

            Type t = this.GetType();

            PropertyInfo prop = t.GetProperty(propertyName);

            if (prop.CanWrite)
            {
                if (Attribute.IsDefined(prop, typeof(ConfigLookupAttribute)))
                {
                    ConfigLookupAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigLookupAttribute)) as ConfigLookupAttribute;
                    Type propertyType = prop.PropertyType;
                    if (propertyType.IsEnum)
                    {
                        string value = Table.GetString(prop.Name);
                        if (!string.IsNullOrEmpty(value))
                        {
                            result = (T)(Enum.Parse(propertyType, value) as Object);
                        }
                    }
                    else if (prop.PropertyType == typeof(int?))
                    {
                        result = (T)(Table.GetInt(prop.Name) as object);
                    }
                    else if (prop.PropertyType == typeof(string))
                    {
                        result = (T)(Table.GetString(prop.Name) as object);
                    }
                    else
                    {
                        //TODO change to log error
                        throw new Exception(prop.PropertyType.ToString() + " is not supported update BasePlugin.LoadFromStorage() to implement support");
                    }
                }
                else if (Attribute.IsDefined(prop, typeof(ConfigNumberAttribute)))
                {
                    ConfigNumberAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigNumberAttribute)) as ConfigNumberAttribute;

                    if (prop.PropertyType == typeof(int?))
                    {
                        result = (T)(Table.GetInt(prop.Name) as object);
                    }
                    else if (prop.PropertyType == typeof(double?))
                    {
                        result = (T)(Table.GetDouble(prop.Name) as object);
                    }
                    else
                    {
                        throw new Exception(prop.PropertyType.ToString() + " is not supported update BasePlugin.LoadFromStorage() to implement support");
                    }
                }
                else if (Attribute.IsDefined(prop, typeof(ConfigStringAttribute)))
                {
                    ConfigStringAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigStringAttribute)) as ConfigStringAttribute;

                    result = (T)(Table.GetString(prop.Name) as object);
                }
                else if (Attribute.IsDefined(prop, typeof(ConfigTableAttribute)))
                {
                    ConfigTableAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigTableAttribute)) as ConfigTableAttribute;

                    //Single Item
                    if (typeof(T).IsInterface)
                    {
                        ITable table = Table.GetTables(prop.Name).FirstOrDefault();

                        if (table != null)
                        {
                            string tableTypeString = table.GetString("Type");
                            if (!string.IsNullOrEmpty(tableTypeString))
                            {
                                //Get type from local project if possible, if not use other loader
                                Type       tableType    = System.Type.GetType(tableTypeString) ?? Runtime.Loader.GetObjectType(tableTypeString);
                                string     id           = table.GetString("Id");
                                BaseRecord createdTable = Activator.CreateInstance(tableType, Table.Database.AddTable(id), Logger, this.Runtime) as BaseRecord;

                                result = (T)(createdTable as object);
                            }
                        }
                    }
                    //List of items
                    else
                    {
                        result = (T)Activator.CreateInstance(typeof(T));
                        foreach (ITable table in Table.GetTables(prop.Name))
                        {
                            string tableTypeString = table.GetString("Type");
                            if (!string.IsNullOrEmpty(tableTypeString))
                            {
                                //Get type from local project if possible, if not use other loader
                                Type   tableType = System.Type.GetType(tableTypeString) ?? Runtime.Loader.GetObjectType(tableTypeString);
                                string id        = table.GetString("Id");
                                try
                                {
                                    BaseRecord createdTable = Activator.CreateInstance(tableType, Table.Database.AddTable(id), Logger, this.Runtime) as BaseRecord;

                                    result.GetType().GetMethod("Add").Invoke(result, new object[] { createdTable });
                                }
                                catch
                                {
                                    // TODO: There is a frequent error here when loading lights plugin where Afterglow.ini is already in use.
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }