Ejemplo n.º 1
0
 /// <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);
             }
         }
     }
 }
Ejemplo n.º 2
0
        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());
        }
Ejemplo n.º 3
0
        /// <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);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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));
        }