/// <summary> /// Reads ASCII encoded AICharacters into this collection from the given stream /// </summary> /// <param name="replaceExisting">Set to true to overwrite AICharacters which already exist in this collection. </param> public void Read(Stream stream, bool replaceExisting) { using (AIReader air = new AIReader(stream)) { AICharacter aic; while ((aic = air.Read <AICharacter>()) != null) { if (this.ContainsKey(aic.Index)) { if (replaceExisting) { this[aic.Index] = aic; } else { throw new Exception(aic.Index + " does already exist in this collection!"); } } else { this.Add(aic.Index, aic); } } } }
static object ReadString(AIReader r, string line, Type valueType) { int index = line.IndexOf('='); if (index >= 0) { return(line.Substring(index + 1).Trim()); } line = r.ReadLine(); if (line == null) { throw new FormatException("EOS"); } if (line != "{") { throw new FormatException("{"); } StringBuilder sb = new StringBuilder(20); while ((line = r.ReadLine()) != null) { if (line == "}") { break; } sb.AppendLine(line); } return(sb.ToString()); }
/// <summary> /// Reads ASCII encoded AICharacters into this collection from the given stream /// </summary> /// <param name="replaceExisting">Set to true to overwrite AICharacters which already exist in this collection. </param> public void Read(Stream stream, bool replaceExisting) { using (AIReader air = new AIReader(stream)) { var readHeader = air.Read <AIFileHeader>(); if (readHeader != null) { this.header = readHeader; } air.Reset(); // for the case that the header was in the middle or not there at all AICharacter aic; while ((aic = air.Read <AICharacter>()) != null) { if (this.ContainsKey(aic.Index)) { if (replaceExisting) { this[aic.Index] = aic; } else { throw new Exception(aic.Index + " does already exist in this collection!"); } } else { this.Add(aic.Index, aic); } } } }
static object ReadEnum(AIReader r, string line, Type valueType) { int index = line.IndexOf('='); if (index < 0) { throw new FormatException("Missing '=' in line " + r.lineNum); } line = line.Substring(index + 1).Trim(); return(Enum.Parse(valueType, line, true)); }
static object ReadInt(AIReader r, string line, Type valueType) { int index = line.IndexOf('='); if (index < 0) { throw new FormatException("Missing '=' in line " + r.lineNum); } line = line.Substring(index + 1).Trim(); if (bool.TryParse(line, out bool value)) // in case the type gets changed between versions { return(value ? 1 : 0); } return(int.Parse(line)); }
static object ReadBool(AIReader r, string line, Type valueType) { int index = line.IndexOf('='); if (index < 0) { throw new FormatException("Missing '=' in line " + r.lineNum); } line = line.Substring(index + 1).Trim(); if (int.TryParse(line, out int value)) { return(value != 0); } return(bool.Parse(line)); }