public IniFileEntryAttribute(IniFiles file, IniSections section, ServerProfileCategory category, string key = "") : base(file, section, category, key) { }
public void Serialize(object obj, ServerProfileCategory[] exclusions) { var iniFiles = new Dictionary<string, IniFile>(); var fields = obj.GetType().GetProperties().Where(f => f.IsDefined(typeof(IniFileEntryAttribute), false)); if (exclusions == null) exclusions = new ServerProfileCategory[0]; foreach (var field in fields) { var attributes = field.GetCustomAttributes(typeof(IniFileEntryAttribute), false).OfType<IniFileEntryAttribute>(); foreach (var attr in attributes) { if (exclusions.Contains(attr.Category)) continue; if (attr.Section == IniFileSections.Custom) { // this code is to handle custom sections var collection = field.GetValue(obj) as IIniSectionCollection; if (collection != null) { collection.Update(); foreach (var section in collection.Sections) { // clear the entire section WriteValue(iniFiles, attr.File, section.IniCollectionKey, null, null); if (section.IsEnabled) { WriteSection(iniFiles, attr.File, section.IniCollectionKey, section.ToIniValues().ToArray()); } } } } else { var value = field.GetValue(obj); var keyName = string.IsNullOrWhiteSpace(attr.Key) ? field.Name : attr.Key; if (attr.ClearSection) { WriteValue(iniFiles, attr.File, attr.Section, null, null); } // // If this is a collection, we need to first remove all of its values from the INI. // var collection = value as IIniValuesCollection; if (collection != null) { var section = ReadSection(iniFiles, attr.File, attr.Section); var filteredSection = section .Where(s => !s.StartsWith(collection.IniCollectionKey + (collection.IsArray ? "[" : "="))) .ToArray(); WriteSection(iniFiles, attr.File, attr.Section, filteredSection); } if (!string.IsNullOrEmpty(attr.ConditionedOn)) { var conditionField = obj.GetType().GetProperty(attr.ConditionedOn); var conditionValue = conditionField.GetValue(obj); if (conditionValue is bool && (bool)conditionValue == false) { // The condition value was not set to true, so clear this attribute instead of writing it WriteValue(iniFiles, attr.File, attr.Section, keyName, null); continue; } } if (!string.IsNullOrEmpty(attr.ClearWhenOff)) { var updateOffField = obj.GetType().GetProperty(attr.ClearWhenOff); var updateOffValue = updateOffField.GetValue(obj); if (updateOffValue is bool && (bool)updateOffValue == false) { // The attributed value was set to false, so clear this attribute instead of writing it WriteValue(iniFiles, attr.File, attr.Section, keyName, null); } continue; } if (attr.WriteBoolValueIfNonEmpty) { if (value == null) { WriteValue(iniFiles, attr.File, attr.Section, keyName, "False"); } else { if (value is string) { var strValue = value as string; WriteValue(iniFiles, attr.File, attr.Section, keyName, string.IsNullOrEmpty(strValue) ? "False" : "True"); } else { // Not supported throw new NotSupportedException("Unexpected IniFileEntry value type."); } } } else { if (collection != null) { if (collection.IsEnabled) { // Remove all the values in the collection with this key name var section = ReadSection(iniFiles, attr.File, attr.Section); var filteredSection = collection.IsArray ? section.Where(s => !s.StartsWith(keyName + "[")) : section.Where(s => !s.StartsWith(keyName + "=")); var result = filteredSection.Concat(collection.ToIniValues()).ToArray(); WriteSection(iniFiles, attr.File, attr.Section, result); } } else { var strValue = StringUtils.GetPropertyValue(value, field, attr); if (attr.QuotedString == QuotedStringType.True) { // add the leading and trailing quotes, if not already have them if (!strValue.StartsWith("\"")) strValue = "\"" + strValue; if (!strValue.EndsWith("\"")) strValue = strValue + "\""; } else if (attr.QuotedString == QuotedStringType.Remove) { // remove the leading and trailing quotes, if any if (strValue.StartsWith("\"")) strValue = strValue.Substring(1); if (strValue.EndsWith("\"")) strValue = strValue.Substring(0, strValue.Length - 1); } if (attr.Multiline) { // substitutes the NewLine string with "\n" strValue = strValue.Replace(Environment.NewLine, @"\n"); } WriteValue(iniFiles, attr.File, attr.Section, keyName, strValue); } } } } } SaveFiles(iniFiles); }
/// <summary> /// Attribute for the IniFile serializer /// </summary> /// <param name="File">The file into which the setting should be serialized.</param> /// <param name="Section">The section in the ini file.</param> /// <param name="Key">The key within the section. Defaults to the same name as the attributed field.</param> public IniFileEntryAttribute(IniFiles file, IniFileSections section, string key = "", ServerProfileCategory category = ServerProfileCategory.Unknown) { this.File = file; this.Section = section; this.Key = key; this.Category = category; this.QuotedString = QuotedStringType.False; }
public void Deserialize(object obj, ServerProfileCategory[] exclusions) { var iniFiles = new Dictionary<string, IniFile>(); var fields = obj.GetType().GetProperties().Where(f => f.IsDefined(typeof(IniFileEntryAttribute), false)); if (exclusions == null) exclusions = new ServerProfileCategory[0]; foreach (var field in fields) { var attributes = field.GetCustomAttributes(typeof(IniFileEntryAttribute), false); foreach (var attr in attributes.OfType<IniFileEntryAttribute>()) { if (exclusions.Contains(attr.Category)) continue; if (attr.Section == IniFileSections.Custom) { // this code is to handle custom sections var collection = field.GetValue(obj) as IIniSectionCollection; if (collection != null) { ReadFile(iniFiles, attr.File); var sectionNames = ReadCustomSectionNames(iniFiles, attr.File); foreach (var sectionName in sectionNames) { var sectionValues = ReadSection(iniFiles, attr.File, sectionName); collection.Add(sectionName, sectionValues); } } } else { var keyName = string.IsNullOrWhiteSpace(attr.Key) ? field.Name : attr.Key; if (attr.WriteBoolValueIfNonEmpty) { // Don't really need to do anything here, we don't care about this on reading it. // extraBoolValue = Convert.ToBoolean(IniReadValue(SectionNames[attr.Section], attr.Key)); } else { var iniValue = ReadValue(iniFiles, attr.File, attr.Section, keyName); var fieldType = field.PropertyType; var collection = field.GetValue(obj) as IIniValuesCollection; if (collection != null) { var section = ReadSection(iniFiles, attr.File, attr.Section); var filteredSection = collection.IsArray ? section.Where(s => s.StartsWith(collection.IniCollectionKey + "[")) : section.Where(s => s.StartsWith(collection.IniCollectionKey + "=")); collection.FromIniValues(filteredSection); } else if (fieldType == typeof(string)) { var stringValue = iniValue; if (attr.QuotedString == QuotedStringType.True) { // remove the leading and trailing quotes, if any if (stringValue.StartsWith("\"")) stringValue = stringValue.Substring(1); if (stringValue.EndsWith("\"")) stringValue = stringValue.Substring(0, stringValue.Length - 1); } else if (attr.QuotedString == QuotedStringType.Remove) { // remove the leading and trailing quotes, if any if (stringValue.StartsWith("\"")) stringValue = stringValue.Substring(1); if (stringValue.EndsWith("\"")) stringValue = stringValue.Substring(0, stringValue.Length - 1); } if (attr.Multiline) { stringValue = stringValue.Replace(@"\n", Environment.NewLine); } field.SetValue(obj, stringValue); } else { // Update the ConditionedOn flag, if this field has one. if (!string.IsNullOrWhiteSpace(attr.ConditionedOn)) { var conditionField = obj.GetType().GetProperty(attr.ConditionedOn); if (string.IsNullOrWhiteSpace(iniValue)) { conditionField.SetValue(obj, false); } else { conditionField.SetValue(obj, true); } } if (string.IsNullOrWhiteSpace(iniValue)) { // Skip non-string values which are not found continue; } var valueSet = StringUtils.SetPropertyValue(iniValue, obj, field, attr); if (!valueSet) throw new ArgumentException($"Unexpected field type {fieldType.ToString()} for INI key {keyName} in section {attr.Section}."); } } } } } }