public override string ToINIValue() { GetPropertyInfos(); if (this.Properties.Count == 0) { return(string.Empty); } var result = new StringBuilder(); result.Append("("); var delimiter = ""; foreach (var prop in this.Properties.OrderBy(p => p.Name)) { result.Append(delimiter); var attr = prop.GetCustomAttributes(typeof(AggregateIniValueEntryAttribute), false).OfType <AggregateIniValueEntryAttribute>().FirstOrDefault(); var propName = string.IsNullOrWhiteSpace(attr?.Key) ? prop.Name : attr.Key; var val = prop.GetValue(this); var propValue = StringUtils.GetPropertyValue(val, prop); result.Append($"{propName}={propValue}"); delimiter = DELIMITER.ToString(); } result.Append(")"); return(result.ToString()); }
public virtual string ToINIValue() { GetPropertyInfos(); if (this.Properties.Count == 0) { return(string.Empty); } var result = new StringBuilder(); result.Append("("); var delimiter = ""; foreach (var prop in this.Properties) { result.Append(delimiter); var attr = prop.GetCustomAttributes(typeof(AggregateIniValueEntryAttribute), false).OfType <AggregateIniValueEntryAttribute>().FirstOrDefault(); var propName = string.IsNullOrWhiteSpace(attr?.Key) ? prop.Name : attr.Key; var val = prop.GetValue(this); var propValue = StringUtils.GetPropertyValue(val, prop); if ((attr?.ExcludeIfEmpty ?? false) && string.IsNullOrWhiteSpace(propValue)) { Debug.WriteLine($"{propName} skipped, ExcludeIfEmpty = true and value is empty"); } else { result.Append($"{propName}={propValue}"); delimiter = DELIMITER.ToString(); } } result.Append(")"); return(result.ToString()); }
public string ToIniValue(NPCSpawnContainerType containerType) { GetPropertyInfos(); if (this.Properties.Count == 0) { return(string.Empty); } var result = new StringBuilder(); var delimiter = ""; foreach (var prop in this.Properties) { var attrSpawn = prop.GetCustomAttributes(typeof(NPCSpawnAttribute), false).OfType <NPCSpawnAttribute>().FirstOrDefault(); if (!attrSpawn?.ContainerTypes?.Contains(containerType) ?? false) { continue; } result.Append(delimiter); var attr = prop.GetCustomAttributes(typeof(AggregateIniValueEntryAttribute), false).OfType <AggregateIniValueEntryAttribute>().FirstOrDefault(); var propName = string.IsNullOrWhiteSpace(attr?.Key) ? prop.Name : attr.Key; result.Append($"{propName}="); if (attr?.ValueWithinBrackets ?? false) { result.Append("("); } var val = prop.GetValue(this); var spawnCollection = val as ISpawnIniValuesCollection; if (spawnCollection != null) { var iniVals = spawnCollection.ToIniValues(containerType); var delimiter2 = ""; foreach (var iniVal in iniVals) { result.Append(delimiter2); if (attr?.ListValueWithinBrackets ?? false) { result.Append($"({iniVal})"); } else { result.Append(iniVal); } delimiter2 = DELIMITER.ToString(); } } else { var collection = val as IIniValuesCollection; if (collection != null) { var iniVals = collection.ToIniValues(); var delimiter2 = ""; foreach (var iniVal in iniVals) { result.Append(delimiter2); if (attr?.ListValueWithinBrackets ?? false) { result.Append($"({iniVal})"); } else { result.Append(iniVal); } delimiter2 = DELIMITER.ToString(); } } else { var propValue = StringUtils.GetPropertyValue(val, prop); result.Append(propValue); } } if (attr?.ValueWithinBrackets ?? false) { result.Append(")"); } delimiter = DELIMITER.ToString(); } return(result.ToString()); }
public void Serialize(object obj) { var iniFiles = new Dictionary <string, IniFile>(); var fields = obj.GetType().GetProperties().Where(f => f.IsDefined(typeof(IniFileEntryAttribute), false)); foreach (var field in fields) { var attributes = field.GetCustomAttributes(typeof(IniFileEntryAttribute), false).OfType <IniFileEntryAttribute>(); foreach (var attr in attributes) { 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) { if (!strValue.StartsWith("\"")) { strValue = "\"" + strValue; } if (!strValue.EndsWith("\"")) { strValue = strValue + "\""; } } 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); }
protected virtual string ToComplexINIValue(bool resultWithinBrackets) { GetPropertyInfos(); if (this.Properties.Count == 0) { return(string.Empty); } var result = new StringBuilder(); if (resultWithinBrackets) { result.Append("("); } var delimiter = ""; foreach (var prop in this.Properties) { var attr = prop.GetCustomAttributes(typeof(AggregateIniValueEntryAttribute), false).OfType <AggregateIniValueEntryAttribute>().FirstOrDefault(); var propName = string.IsNullOrWhiteSpace(attr?.Key) ? prop.Name : attr.Key; var val = prop.GetValue(this); var collection = val as IIniValuesCollection; if (collection != null) { result.Append(delimiter); result.Append($"{propName}="); if (attr?.ValueWithinBrackets ?? false) { result.Append("("); } var iniVals = collection.ToIniValues(); var delimiter2 = ""; foreach (var iniVal in iniVals) { result.Append(delimiter2); if (attr?.ListValueWithinBrackets ?? false) { result.Append($"({iniVal})"); } else { result.Append(iniVal); } delimiter2 = DELIMITER.ToString(); } if (attr?.ValueWithinBrackets ?? false) { result.Append(")"); } delimiter = DELIMITER.ToString(); } else { if ((attr?.ExcludeIfEmpty ?? false) && val is string && string.IsNullOrWhiteSpace(val.ToString())) { Debug.WriteLine($"{propName} skipped, ExcludeIfEmpty = true and value is null or empty"); } else if ((attr?.ExcludeIfFalse ?? false) && val is bool && !((bool)val)) { Debug.WriteLine($"{propName} skipped, ExcludeIfFalse = true and value is false"); } else { var propValue = StringUtils.GetPropertyValue(val, prop); result.Append(delimiter); result.Append($"{propName}="); if (attr?.ValueWithinBrackets ?? false) { result.Append("("); } result.Append(propValue); if (attr?.ValueWithinBrackets ?? false) { result.Append(")"); } delimiter = DELIMITER.ToString(); } } } if (resultWithinBrackets) { result.Append(")"); } return(result.ToString()); }