Esempio n. 1
0
        private static AdmBlock ReadBlock(string name, ref AdmConverterContext context)
        {
            AdmBlock block = new AdmBlock();
            block.NameHash = name.StartsWith("0x") ? int.Parse(name.Remove(0, 2), NumberStyles.HexNumber) : HashUtility.GenerateHash(name);

            string line = "";
            while ((line = context.Reader.ReadLine()) != null)
            {
                context.LineNumber++;

                if (line.StartsWith("//") || line.StartsWith("#"))
                {
                    continue;
                }
                else if (line.StartsWith("<") && line.IndexOf(">") != -1 && line.IndexOf(":") != -1)
                {
                    block.Fields.Add(ReadField(line, ref context));
                }
                else if (line.StartsWith("[") && line.EndsWith("]"))
                {
                    if (line.StartsWith("[/"))
                    {
                        line = line.Substring(2, line.Length - 3);

                        if (line != context.BlockStack.Pop())
                        {
                            throw new AdmConverterException(String.Format("Can't close outer block before inner! (Line: {0})", context.LineNumber));
                        }

                        break;
                    }

                    line = line.Substring(1, line.Length - 2);

                    context.BlockStack.Push(line);

                    block.Childs.Add(ReadBlock(line, ref context));
                }
                else
                {
                    throw new AdmConverterException(String.Format("Unknown line format! (Line: {0})", context.LineNumber));
                }
            }

            return block;
        }
Esempio n. 2
0
        public static AdmData GetData(string data)
        {
            AdmData admData = new AdmData();

            var context = new AdmConverterContext();
            context.Reader = new StringReader(data);

            string line = "";

            while ((line = context.Reader.ReadLine()) != null)
            {
                context.LineNumber++;

                if (line.StartsWith("[") && line.EndsWith("]"))
                {
                    if (line.StartsWith("[/"))
                    {
                        line = line.Substring(2, line.Length - 3);

                        if (line != context.BlockStack.Pop())
                        {
                            throw new AdmConverterException(String.Format("Can't close outer block before inner! (Line: {0})", context.LineNumber));
                        }

                        break;
                    }

                    line = line.Substring(1, line.Length - 2);

                    context.BlockStack.Push(line);

                    admData.MainBlock = ReadBlock(line, ref context);
                }
            }


            context.Reader.Close();
            context.Reader.Dispose();

            return admData;
        }
Esempio n. 3
0
        private static AdmField ReadField(string line, ref AdmConverterContext context)
        {
            string type = line.Substring(1, line.IndexOf(">") - 1);

            var field = GetFieldByType(type);

            if (field == null)
            {
                throw new AdmConverterException(String.Format("Invalid field type! (Line: {0})", context.LineNumber));
            }

            string name = line.Substring(line.IndexOf(">") + 1, line.IndexOf(":") - line.IndexOf(">") - 1);

            field.NameHash = name.StartsWith("0x") ? int.Parse(name.Remove(0,2), NumberStyles.HexNumber) : HashUtility.GenerateHash(name);

            string value = line.Substring(line.IndexOf(":") + 1);
            try
            {
                switch (field.Type)
                {
                    case AdmFieldType.Integer:
                        field.Value = ReadInteger(value);
                        break;
                    case AdmFieldType.Float:
                        field.Value = ReadFloat(value);
                        break;
                    case AdmFieldType.Double:
                        field.Value = ReadDouble(value);
                        break;
                    case AdmFieldType.UnsignedInteger:
                        field.Value = ReadUnsignedInt(value);
                        break;
                    case AdmFieldType.String:
                        field.Value = ReadString(value);
                        break;
                    case AdmFieldType.Boolean:
                        field.Value = ReadBoolean(value);
                        break;
                    case AdmFieldType.Integer64:
                        field.Value = ReadInteger64(value);
                        break;
                    case AdmFieldType.Translate:
                        field.Value = ReadTranslate(value);
                        break;
                }
            }
            catch (InvalidDataException e)
            {
                throw new AdmConverterException(string.Format("{0} (Line: {1}", e.Message, context.LineNumber));
            }

            return field;
        }