public Section(string file, BinaryReader reader, BiniStringBlock stringBlock) { if (reader == null) { throw new ArgumentNullException("reader"); } if (stringBlock == null) { throw new ArgumentNullException("stringBlock"); } File = file; short nameOffset = reader.ReadInt16(); Name = stringBlock.Get(nameOffset); short count = reader.ReadInt16(); entries = new List <Entry>(count); for (int i = 0; i < count; i++) { entries.Add(new Entry(file, reader, stringBlock)); } }
public StringValue(BinaryReader reader, BiniStringBlock stringBlock) { if (reader == null) { throw new ArgumentNullException("reader"); } if (stringBlock == null) { throw new ArgumentNullException("stringBlock"); } this.valuePointer = reader.ReadInt32(); this.value = stringBlock.Get(valuePointer); }
public Entry(string file, BinaryReader reader, BiniStringBlock stringBlock) { if (reader == null) { throw new ArgumentNullException("reader"); } if (stringBlock == null) { throw new ArgumentNullException("stringBlock"); } File = file; short nameOffset = reader.ReadInt16(); Name = stringBlock.Get(nameOffset); byte count = reader.ReadByte(); values = new List <IValue>(count); for (int i = 0; i < count; i++) { IniValueType valueType = (IniValueType)reader.ReadByte(); switch (valueType) { case IniValueType.Boolean: values.Add(new BooleanValue(reader)); break; case IniValueType.Int32: values.Add(new Int32Value(reader)); break; case IniValueType.Single: values.Add(new SingleValue(reader)); break; case IniValueType.String: values.Add(new StringValue(reader, stringBlock)); break; default: throw new FileContentException(BINI, "Unknown BINI value type: " + valueType); } } }
protected IEnumerable <Section> ParseFile(string path, MemoryStream stream, bool preparse = true, bool allowmaps = false) { if (string.IsNullOrEmpty(path)) { path = "[Memory]"; } stream.Position = 0; byte[] buffer = new byte[4]; stream.Read(buffer, 0, 4); string fileType = Encoding.ASCII.GetString(buffer); Section currentSection = null; if (fileType == FileType) // Binary Ini { BinaryReader reader = new BinaryReader(stream); int formatVersion = reader.ReadInt32(); if (formatVersion != FileVersion) { throw new FileVersionException(path, fileType, formatVersion, FileVersion); } int stringBlockOffset = reader.ReadInt32(); if (stringBlockOffset > reader.BaseStream.Length) { throw new FileContentException(path, fileType, "The string block offset was out of range: " + stringBlockOffset); } long sectionBlockOffset = reader.BaseStream.Position; reader.BaseStream.Seek(stringBlockOffset, SeekOrigin.Begin); Array.Resize <byte>(ref buffer, (int)(reader.BaseStream.Length - stringBlockOffset)); reader.Read(buffer, 0, buffer.Length); var stringBlock = new BiniStringBlock(Encoding.ASCII.GetString(buffer)); reader.BaseStream.Seek(sectionBlockOffset, SeekOrigin.Begin); while (reader.BaseStream.Position < stringBlockOffset) { yield return(new Section(path, reader, stringBlock)); } } else // Text Ini { stream.Seek(0, SeekOrigin.Begin); StreamReader reader = new StreamReader(stream); int currentLine = 0; bool inSection = false; string[] parts = new string[3]; while (!reader.EndOfStream) { currentLine++; string line = reader.ReadLine().Trim(); if (string.IsNullOrWhiteSpace(line) || line[0] == ';' || line[0] == '@') { continue; } if (line[0] == '[') { if (currentSection != null) { yield return(currentSection); } int indexComment = line.IndexOf(';'); int indexClose = line.IndexOf(']'); if (indexComment != -1 && indexComment < indexClose) { inSection = false; currentSection = null; continue; } if (indexClose == -1) { throw new FileContentException(path, IniFileType, "Invalid section header: " + line); } string name = line.Substring(1, indexClose - 1).Trim(); currentSection = new Section(name) { File = path, Line = currentLine }; inSection = true; continue; } else { if (!inSection) { continue; } int indexComment = line.IndexOf(';'); if (indexComment != -1) { line = line.Remove(indexComment); } int partCount; if (!char.IsLetterOrDigit(line[0]) && line[0] != '_') { FLLog.Warning("Ini", "Invalid line in file: " + path + " at line " + currentLine + '"' + line + '"'); } else { partCount = ParseEquals(line, parts, allowmaps); if (partCount == 2) { string val = parts[1]; string[] valParts = val.Split(','); List <IValue> values = new List <IValue>(valParts.Length); foreach (string part in valParts) { string s = part.Trim(); bool tempBool; float tempFloat; long tempLong; if (s.Length == 0) { values.Add(new StringValue("")); continue; } if (preparse && (s[0] == '-' || s[0] >= '0' && s[0] <= '9')) { if (long.TryParse(s, out tempLong)) { if (tempLong >= int.MinValue && tempLong <= int.MaxValue) { values.Add(new Int32Value((int)tempLong)); } else { values.Add(new SingleValue(tempLong, tempLong)); } } else if (float.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out tempFloat)) { values.Add(new SingleValue(tempFloat, null)); } else { values.Add(new StringValue(s)); } } else if (preparse && bool.TryParse(s, out tempBool)) { values.Add(new BooleanValue(tempBool)); } else { values.Add(new StringValue(s)); } } currentSection.Add(new Entry(parts[0].Trim(), values) { File = path, Line = currentLine }); } else if (partCount == 3 && allowmaps) { string k = parts[1].Trim(); string v = parts[2].Trim(); currentSection.Add(new Entry(parts[0].Trim(), new IValue[] { new StringKeyValue(k, v) }) { File = path, Line = currentLine }); } else if (partCount == 1) { currentSection.Add(new Entry(parts[0].Trim(), new List <IValue>()) { File = path, Line = currentLine }); } } } } } if (currentSection != null) { yield return(currentSection); } }