public void Set <T>(string name, T value, IConfigConverter converter, string category = null, string description = null) { string key = $"{(category != null ? category.Replace(SPACE, string.Empty) + "." : string.Empty)}{name.Replace(SPACE, string.Empty)}"; if (converter == null) { if (configs.ContainsKey(key)) { configs[key] = value.ToString(); } else { configs.Add(key, value.ToString()); } } else { if (configs.ContainsKey(key)) { configs[key] = converter.ConvertFromValue(value); } else { configs.Add(key, converter.ConvertFromValue(value)); } } if (infos.ContainsKey(key)) { infos.Add(key, new ConfigInfo(name, description, category)); } else { infos[key] = new ConfigInfo(name, description, category); } }
// 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; } } } } }