public void SetValue <T>(string name, T value, string groupName = null) { SavedGroup foundGroup = GetGroup(groupName, true); if (foundGroup != null) { bool updatedElement = false; foreach (SavedElement element in foundGroup.elements) { if (element.name == name) { element.value = value; updatedElement = true; } } if (!updatedElement) { foundGroup.elements.Add(new SavedElement() { name = name, value = value }); } } }
internal SavedGroup GetGroup(string name, bool createIfNotExists = false) { SavedGroup returnGroup = null; if (name == null) { name = emptyGroupName; } foreach (SavedGroup group in SavedData) { if (group.name == name) { returnGroup = group; } } if (returnGroup == null && (createIfNotExists || name == emptyGroupName)) { returnGroup = new SavedGroup() { name = name }; SavedData.Add(returnGroup); } return(returnGroup); }
public OptionError GetValue <T>(string name, ref T value, string groupName = null, bool strict = true) { SavedElement foundElement = null; SavedGroup foundGroup = GetGroup(groupName); OptionError error = OptionError.NoError; if (foundGroup != null) { foreach (SavedElement element in foundGroup.elements) { if (element.name == name) { foundElement = element; break; } } if (foundElement != null) { try { if (strict) { if (foundElement.value.GetType() == typeof(T)) { value = (T)Convert.ChangeType(foundElement.value, typeof(T)); } else { error = OptionError.CastFailed; } } else { value = (T)Convert.ChangeType(foundElement.value, typeof(T)); } } catch { error = OptionError.CastFailed; } } else { error = OptionError.OptionNotFound; } } else { error = OptionError.GroupNotFound; } return(error); }
public Dictionary <string, T> GetValues <T>(string groupName, bool strict = true) { Dictionary <string, T> returnValues = new Dictionary <string, T>(); SavedGroup foundGroup = GetGroup(groupName); if (foundGroup != null) { foreach (SavedElement element in foundGroup.elements) { T valueOut = default(T); if (GetValue <T>(element.name, ref valueOut, groupName, strict) == OptionError.NoError) { returnValues.Add(element.name, valueOut); } } } return(returnValues); }