// POPULATES EVERY CLASS WITH THE CONFIG VALUES. THESE VALUES CAN STILL BE OBTAINED IN A CONVENTIONAL WAY THROUGH FILES internal static void Populate() { foreach (Assembly modDll in cachedModDlls) { foreach (Type type in modDll.GetTypes()) { foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { object[] attributes = field.GetCustomAttributes(typeof(ConfigValueAttribute), false); ConfigValueAttribute configValue = attributes.Length > 0 ? attributes[0] as ConfigValueAttribute : null; attributes = field.GetCustomAttributes(typeof(ConfigDescriptionAttribute), false); ConfigDescriptionAttribute description = attributes.Length > 0 ? attributes[0] as ConfigDescriptionAttribute : null; attributes = field.GetCustomAttributes(typeof(ConfigCategoryAttribute), false); ConfigCategoryAttribute category = attributes.Length > 0 ? attributes[0] as ConfigCategoryAttribute : null; attributes = field.GetCustomAttributes(typeof(ConfigConverterAttribute), false); ConfigConverterAttribute converter = attributes.Length > 0 ? attributes[0] as ConfigConverterAttribute : null; attributes = field.GetCustomAttributes(typeof(ConfigUIAttribute), false); ConfigUIAttribute uiDesign = attributes.Length > 0 ? attributes[0] as ConfigUIAttribute : null; if (configValue == null) { continue; } ConfigFile file; FileID id = new FileID(modDll, configValue.file == null ? "main.config" : configValue.file + ".config"); if (!files.ContainsKey(id)) { file = new ConfigFile(modDll, configValue.file == null ? "main.config" : configValue.file + ".config"); files.Add(id, file); } else { file = files[id]; } IConfigConverter convert = converter?.converter; if (uiDesign != null) { file.AddDesign(configValue.name, uiDesign, category?.category); } string value = file.Get(configValue.name, convert != null ? convert.ConvertFromValue(configValue.defaultValue) : configValue.defaultValue.ToString(), null, category?.category, description?.description); try { field.SetValue(null, Convert.ChangeType(value, field.FieldType)); } catch (Exception e) { SRML.Console.LogError($"Trying to set value for field '{field.Name}' from config, but config value can't be converted. Default value will be used, however this might mean a field is asking for a diferent type of data then it can contain"); UnityEngine.Debug.LogException(e); field.SetValue(null, configValue.defaultValue); continue; } } } } }
public static bool ShowOptionsWindow(FrameworkElement ChatControl, RatChat.Core.ConfigStorage ChatConfigStorage) { ChatOptionsWindow cow = new ChatOptionsWindow(); var data = ChatControl.Tag as Tuple <RatChat.Core.IChatSource, string>; var configs = (from a in ConfigValueAttribute.GetAttribute(data.Item1.GetType()) orderby a.Caption select a).ToArray(); for (int j = 0; j < configs.Length; ++j) { cow.OptionsGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(30.0) }); // add text, TextBlock text = new TextBlock() { Text = configs[j].Caption }; text.SetResourceReference(TextBlock.StyleProperty, "ConfigText"); cow.OptionsGrid.Children.Add(text); Grid.SetRow(text, j); // add textbox UIElement val = null; if (configs[j].IsPasswordInput) { val = new PasswordBox() { Tag = data.Item1.ConfigPrefix + configs[j].Name, Margin = new Thickness(2) }; ((PasswordBox)val).Password = (string)ChatConfigStorage.GetDefault(data.Item1.ConfigPrefix + configs[j].Name, configs[j].DefaultValue); } else { val = new TextBox() { Tag = data.Item1.ConfigPrefix + configs[j].Name, Margin = new Thickness(2) }; ((TextBox)val).Text = (string)ChatConfigStorage.GetDefault(data.Item1.ConfigPrefix + configs[j].Name, configs[j].DefaultValue); } cow.OptionsGrid.Children.Add(val); Grid.SetRow(val, j); Grid.SetColumn(val, 1); } bool?ret = cow.ShowDialog(); if (ret.HasValue && ret.Value) { // save for (int j = 0; j < cow.OptionsGrid.Children.Count; ++j) { TextBox val = cow.OptionsGrid.Children[j] as TextBox; if (val != null) { string name = val.Tag as string; ChatConfigStorage[name] = val.Text; } else { PasswordBox pb = cow.OptionsGrid.Children[j] as PasswordBox; if (pb != null) { string name = pb.Tag as string; ChatConfigStorage[name] = pb.Password; } } } return(true); } return(false); }