private string GetCSharpType(
            TableDescriptor.StructDef.FieldDef fieldDef)
        {
            string csharpType = "";

            if (fieldDef.Type == FieldType.Int)
            {
                csharpType = "int";
            }
            else if (fieldDef.Type == FieldType.String)
            {
                csharpType = "string";
            }

            return(csharpType);
        }
        private string GetCSharpTypeDefaultValue(
            TableDescriptor.StructDef.FieldDef fieldDef)
        {
            string defaultValue = "";

            if (fieldDef.Type == FieldType.Int)
            {
                defaultValue = "0";
            }
            else if (fieldDef.Type == FieldType.String)
            {
                defaultValue = "\"\"";
            }

            return(defaultValue);
        }
        private void GetStructDeclFieldDecl(
            TableDescriptor.StructDef structDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < structDef.Fields.Count; ++i)
            {
                TableDescriptor.StructDef.FieldDef fieldDef =
                    structDef.Fields[i];

                string csharpType   = GetCSharpType(fieldDef);
                string defaultValue = GetCSharpTypeDefaultValue(fieldDef);
                sb.AppendFormat("{0}public {1} {2} = {3};{4}",
                                indent, csharpType, fieldDef.Name, defaultValue,
                                this.newLineStr);
            }

            output = sb.ToString();
        }
        private void GetStructDeclParseFunc(
            TableDescriptor.StructDef structDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            string start = string.Format(
                "{0}public override bool Parse(string text){1}" +
                "{0}{{{1}",
                indent, this.newLineStr);
            string end = string.Format(
                "{0}}}{1}",
                indent, this.newLineStr);

            sb.Append(start);

            indent += "    ";

            if (structDef.Fields.Count == 0)
            {
                sb.AppendFormat(
                    "{0}return true;{1}",
                    indent, this.newLineStr);
            }
            else
            {
                sb.AppendFormat(
                    "{0}Brickred.Table.ColumnSpliter s ={1}" +
                    "{0}    new Brickred.Table.ColumnSpliter(text, ';');{1}" +
                    "{1}",
                    indent, this.newLineStr);

                for (int i = 0; i < structDef.Fields.Count; ++i)
                {
                    TableDescriptor.StructDef.FieldDef fieldDef =
                        structDef.Fields[i];

                    if (fieldDef.Type == FieldType.Int)
                    {
                        sb.AppendFormat(
                            "{0}if (s.NextInt(ref this.{1}) == false) {{{2}" +
                            "{0}    return false;{2}" +
                            "{0}}}{2}",
                            indent, fieldDef.Name, this.newLineStr);
                    }
                    else if (fieldDef.Type == FieldType.String)
                    {
                        sb.AppendFormat(
                            "{0}if (s.NextString(ref this.{1}) == false) {{{2}" +
                            "{0}    return false;{2}" +
                            "{0}}}{2}",
                            indent, fieldDef.Name, this.newLineStr);
                    }
                }

                sb.AppendFormat(
                    "{0}if (s.NextString()) {{{1}" +
                    "{0}    return false;{1}" +
                    "{0}}}{1}" +
                    "{1}" +
                    "{0}return true;{1}",
                    indent, this.newLineStr);
            }

            sb.Append(end);

            output = sb.ToString();
        }
示例#5
0
        private bool AddStructFieldDef(
            TableDescriptor.StructDef structDef, XElement element)
        {
            // check name attr
            string name;

            {
                XAttribute attr = element.Attribute("name");
                if (attr == null)
                {
                    PrintLineError(element,
                                   "`field` node must contain a `name` attribute");
                    return(false);
                }
                name = attr.Value;
            }
            if (Regex.IsMatch(name, @"^[a-zA-Z_]\w*$") == false)
            {
                PrintLineError(element,
                               "`field` node `name` attribute is invalid");
                return(false);
            }
            if (structDef.FieldNameIndex.ContainsKey(name))
            {
                PrintLineError(element,
                               "`field` node `name` attribute duplicated");
                return(false);
            }

            // check type attr
            string type;

            {
                XAttribute attr = element.Attribute("type");
                if (attr == null)
                {
                    PrintLineError(element,
                                   "`field` node must contain a `type` attribute");
                    return(false);
                }
                type = attr.Value;
            }

            TableDescriptor.StructDef.FieldDef def =
                new TableDescriptor.StructDef.FieldDef();
            def.ParentRef  = structDef;
            def.Name       = name;
            def.LineNumber = GetLineNumber(element);

            if (type == "int")
            {
                def.Type = TableDescriptor.StructDef.FieldType.Int;
            }
            else if (type == "string")
            {
                def.Type = TableDescriptor.StructDef.FieldType.String;
            }
            else
            {
                PrintLineError(element,
                               "type `{0}` is invalid", type);
                return(false);
            }

            structDef.Fields.Add(def);
            structDef.FieldNameIndex.Add(def.Name, def);

            return(true);
        }