public NumberControl(PropertyInfo prop, IAfterglowPlugin plugin) : base(prop) { ConfigNumberAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigNumberAttribute)) as ConfigNumberAttribute; if (configAttribute == null) { throw new Exception("prop is not a ConfigNumberAttribute"); } this._propertyInfo = prop; this._plugin = plugin; //Create value Text Box _valueNumericUpDown = new NumericUpDown(); _valueNumericUpDown.Name = Guid.NewGuid().ToString(); _valueNumericUpDown.Minimum = Convert.ToDecimal(configAttribute.Min); _valueNumericUpDown.Maximum = Convert.ToDecimal(configAttribute.Max); //Configure decimal Places if (prop.PropertyType == typeof(int?)) { _valueNumericUpDown.DecimalPlaces = 0; } else if (configAttribute.Min != 0 || configAttribute.Max != 0) { List <int> decimalPlaces = new List <int> { configAttribute.Min.ToString().SkipWhile(c => c != '.').Skip(1).Count(), configAttribute.Max.ToString().SkipWhile(c => c != '.').Skip(1).Count() }; _valueNumericUpDown.DecimalPlaces = decimalPlaces.OrderBy(d => d).FirstOrDefault(); } decimal value = 0; value = Convert.ToDecimal(prop.GetValue(plugin, null)); _valueNumericUpDown.Value = value; _valueNumericUpDown.ValueChanged += new EventHandler(_valueNumericUpDown_ValueChanged); this.Controls.Add(_valueNumericUpDown); InitializeComponent(); }
/// <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); }