private void GetTableDeclParseFuncReadNameLine(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            string start = string.Format(
                "{0}// read name line{1}" +
                "{0}lineBuffer = r.NextLine();{1}" +
                "{0}if (lineBuffer == null) {{{1}" +
                "{0}    errorInfo = \"name line is required\";{1}" +
                "{0}    return false;{1}" +
                "{0}}}{1}" +
                "{0}if (lineBuffer.Count != columnCountReq) {{{1}" +
                "{0}    errorInfo = string.Format({1}" +
                "{0}        \"name line column count {{0}} is invalid," +
                " should be {{1}}\",{1}" +
                "{0}         lineBuffer.Count, columnCountReq);{1}" +
                "{0}    return false;{1}" +
                "{0}}}{1}" +
                "{0}{{{1}" +
                "{0}    int colNumber = 0;{1}" +
                "{1}",
                indent, this.newLineStr);
            string end = string.Format(
                "{0}}}{1}" +
                "{1}",
                indent, this.newLineStr);

            sb.Append(start);

            indent += "    ";
            for (int i = 0; i < tableDef.Columns.Count; ++i)
            {
                TableDescriptor.TableDef.ColumnDef columnDef =
                    tableDef.Columns[i];

                sb.AppendFormat(
                    "{0}if (lineBuffer[colNumber++] != \"{1}\") {{{2}" +
                    "{0}    errorInfo = string.Format({2}" +
                    "{0}        \"column {{0}} should be named as `{1}`\"," +
                    " colNumber);{2}" +
                    "{0}    return false;{2}" +
                    "{0}}}{2}",
                    indent, columnDef.Name, this.newLineStr);
            }

            sb.Append(end);

            output = sb.ToString();
        }
Exemple #2
0
 private void CalculateTableKeyColumnIndex(
     TableDescriptor.TableDef tableDef)
 {
     for (int i = 0; i < tableDef.Columns.Count; ++i)
     {
         TableDescriptor.TableDef.ColumnDef columnDef =
             tableDef.Columns[i];
         if (columnDef == tableDef.TableKey)
         {
             tableDef.TableKeyColumnIndex = i;
             return;
         }
     }
 }
        private void GetTableDeclRowClassDecl(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            string start = string.Format(
                "{0}public sealed class Row{1}" +
                "{0}{{{1}",
                indent, this.newLineStr);
            string end = string.Format(
                "{0}}}{1}",
                indent, this.newLineStr);

            indent += "    ";

            sb.Append(start);

            for (int i = 0; i < tableDef.Columns.Count; ++i)
            {
                TableDescriptor.TableDef.ColumnDef columnDef =
                    tableDef.Columns[i];

                string csharpType   = GetCSharpType(columnDef);
                string defaultValue = GetCSharpTypeDefaultValue(columnDef);
                if (defaultValue.Length > 10)
                {
                    sb.AppendFormat(
                        "{0}public {1} {2} ={4}" +
                        "{0}    {3};{4}",
                        indent, csharpType, columnDef.Name, defaultValue,
                        this.newLineStr);
                }
                else
                {
                    sb.AppendFormat("{0}public {1} {2} = {3};{4}",
                                    indent, csharpType, columnDef.Name, defaultValue,
                                    this.newLineStr);
                }
            }

            sb.Append(end);

            output = sb.ToString();
        }
        private string GetCSharpTypeDefaultValue(
            TableDescriptor.TableDef.ColumnDef columnDef)
        {
            string defaultValue = "";

            if (columnDef.Type == ColumnType.Int)
            {
                defaultValue = "0";
            }
            else if (columnDef.Type == ColumnType.String)
            {
                defaultValue = "\"\"";
            }
            else if (columnDef.Type == ColumnType.Struct ||
                     columnDef.Type == ColumnType.List)
            {
                defaultValue = string.Format("new {0}()",
                                             GetCSharpType(columnDef));
            }

            return(defaultValue);
        }
        private string GetCSharpType(
            TableDescriptor.TableDef.ColumnDef columnDef)
        {
            ColumnType checkType;

            if (columnDef.Type == ColumnType.List)
            {
                checkType = columnDef.ListType;
            }
            else
            {
                checkType = columnDef.Type;
            }

            string csharpType = "";

            if (checkType == ColumnType.Int)
            {
                csharpType = "int";
            }
            else if (checkType == ColumnType.String)
            {
                csharpType = "string";
            }
            else if (checkType == ColumnType.Struct)
            {
                csharpType = columnDef.RefStructDef.Name;
            }

            if (columnDef.Type == ColumnType.List)
            {
                return(string.Format("System.Collections.Generic.List<{0}>",
                                     csharpType));
            }
            else
            {
                return(csharpType);
            }
        }
        private void GetTableDeclParseFuncParseColumns(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < tableDef.Columns.Count; ++i)
            {
                TableDescriptor.TableDef.ColumnDef columnDef =
                    tableDef.Columns[i];

                ColumnType checkType;
                if (columnDef.Type == ColumnType.List)
                {
                    checkType = columnDef.ListType;
                }
                else
                {
                    checkType = columnDef.Type;
                }
                bool isList = (columnDef.Type == ColumnType.List);

                if (checkType == ColumnType.Int)
                {
                    if (isList)
                    {
                        sb.AppendFormat(
                            "{0}Brickred.Table.Util.ReadColumnIntList(" +
                            "lineBuffer[colNumber++], ref row.{1});{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                    else
                    {
                        sb.AppendFormat(
                            "{0}row.{1} = Brickred.Table.Util.Atoi(" +
                            "lineBuffer[colNumber++]);{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                }
                else if (checkType == ColumnType.String)
                {
                    if (isList)
                    {
                        sb.AppendFormat(
                            "{0}Brickred.Table.Util.ReadColumnStringList(" +
                            "lineBuffer[colNumber++], ref row.{1});{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                    else
                    {
                        sb.AppendFormat(
                            "{0}row.{1} = lineBuffer[colNumber++];{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                }
                else if (checkType == ColumnType.Struct)
                {
                    if (isList)
                    {
                        sb.AppendFormat(
                            "{0}if (Brickred.Table.Util.ReadColumnStructList({1}" +
                            "{0}        lineBuffer[colNumber++], ref row.{2}) " +
                            "== false) {{{1}" +
                            "{0}    errorInfo = string.Format({1}" +
                            "{0}        \"line {{0}} column `{2}` value is invalid\", " +
                            "lineNumber);{1}" +
                            "{0}    return false;{1}" +
                            "{0}}}{1}",
                            indent, this.newLineStr, columnDef.Name);
                    }
                    else
                    {
                        sb.AppendFormat(
                            "{0}if (row.{1}.Parse(" +
                            "lineBuffer[colNumber++]) == false) {{{2}" +
                            "{0}    errorInfo = string.Format({2}" +
                            "{0}        \"line {{0}} column `{1}` value is invalid\", " +
                            "lineNumber);{2}" +
                            "{0}    return false;{2}" +
                            "{0}}}{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                }
            }

            output = sb.ToString();
        }
Exemple #7
0
        private bool AddTableColumnDef(
            TableDescriptor.TableDef tableDef, XElement element)
        {
            // check name attr
            string name;

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

            // check type attr
            string type;

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

            TableDescriptor.TableDef.ColumnDef def =
                new TableDescriptor.TableDef.ColumnDef();
            def.ParentRef  = tableDef;
            def.Name       = name;
            def.LineNumber = GetLineNumber(element);

            // get type info
            string columnTypeStr = type;

            {
                Match m = Regex.Match(type, @"^list{(.+)}$");
                if (m.Success)
                {
                    columnTypeStr = m.Groups[1].Value;
                    def.Type      = TableDescriptor.TableDef.ColumnType.List;
                }
            }

            TableDescriptor.TableDef.ColumnType columnType =
                TableDescriptor.TableDef.ColumnType.None;
            if (columnTypeStr == "int")
            {
                columnType = TableDescriptor.TableDef.ColumnType.Int;
            }
            else if (columnTypeStr == "string")
            {
                columnType = TableDescriptor.TableDef.ColumnType.String;
            }
            else
            {
                for (;;)
                {
                    TableDescriptor.StructDef refStructDef = null;

                    // check is local struct
                    if (tableDef.LocalStructNameIndex.TryGetValue(
                            columnTypeStr, out refStructDef))
                    {
                        columnType       = TableDescriptor.TableDef.ColumnType.Struct;
                        def.RefStructDef = refStructDef;
                        break;
                    }

                    // check is global struct
                    if (this.descriptor.GlobalStructNameIndex.TryGetValue(
                            columnTypeStr, out refStructDef))
                    {
                        columnType       = TableDescriptor.TableDef.ColumnType.Struct;
                        def.RefStructDef = refStructDef;
                        break;
                    }

                    PrintLineError(element,
                                   "type `{0}` is undefined", columnTypeStr);
                    return(false);
                }
            }

            if (def.Type == TableDescriptor.TableDef.ColumnType.List)
            {
                def.ListType = columnType;
            }
            else
            {
                def.Type = columnType;
            }

            // check readby attr
            {
                XAttribute attr = element.Attribute("readby");
                if (attr != null)
                {
                    string[] readers = attr.Value.Split('|');
                    for (int i = 0; i < readers.Length; ++i)
                    {
                        string reader = readers[i];

                        TableDescriptor.ReaderDef readerDef = null;
                        if (this.descriptor.Readers.TryGetValue(
                                reader, out readerDef) == false)
                        {
                            PrintLineError(element,
                                           "reader `{0}` is not defined", reader);
                            return(false);
                        }
                        def.Readers[reader] = readerDef;
                    }
                }
            }

            tableDef.Columns.Add(def);
            tableDef.ColumnNameIndex.Add(def.Name, def);

            return(true);
        }
Exemple #8
0
        private bool AddTableDef(XElement element)
        {
            // check name attr
            string name;

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

            TableDescriptor.TableDef def =
                new TableDescriptor.TableDef();
            def.Name       = name;
            def.LineNumber = GetLineNumber(element);

            {
                IEnumerator <XElement> iter =
                    element.Elements().GetEnumerator();
                while (iter.MoveNext())
                {
                    XElement childElement = iter.Current;

                    if (childElement.Name == "struct")
                    {
                        // parse local struct
                        if (AddStructDef(def, childElement) == false)
                        {
                            return(false);
                        }
                    }
                    else if (childElement.Name == "col")
                    {
                        // parse column
                        if (AddTableColumnDef(def, childElement) == false)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        PrintLineError(childElement,
                                       "expect a `struct` or `col` node");
                        return(false);
                    }
                }
            }

            // check key/setkey attr
            {
                string key;

                XAttribute attr = element.Attribute("key");
                if (attr != null)
                {
                    key = attr.Value;
                    def.TableKeyType =
                        TableDescriptor.TableDef.KeyType.SingleKey;
                }
                else
                {
                    attr = element.Attribute("setkey");
                    if (attr != null)
                    {
                        key = attr.Value;
                        def.TableKeyType =
                            TableDescriptor.TableDef.KeyType.SetKey;
                    }
                    else
                    {
                        PrintLineError(element,
                                       "`table` node must contain a " +
                                       "`key` or `setkey` attribute");
                        return(false);
                    }
                }

                TableDescriptor.TableDef.ColumnDef tableKey = null;
                if (def.ColumnNameIndex.TryGetValue(
                        key, out tableKey) == false)
                {
                    PrintLineError(element,
                                   "table key `{0}` is not defined", key);
                    return(false);
                }
                if (tableKey.Type != TableDescriptor.TableDef.ColumnType.Int &&
                    tableKey.Type != TableDescriptor.TableDef.ColumnType.String)
                {
                    PrintLineError(element,
                                   "table key can only be `int` or `string` type");
                    return(false);
                }
                def.TableKey = tableKey;
            }

            // check file attr
            {
                XAttribute attr = element.Attribute("file");
                if (attr == null)
                {
                    PrintLineError(element,
                                   "`table` node must contain a `file` attribute");
                    return(false);
                }
                def.FileName = attr.Value;
            }

            // check readby attr
            {
                XAttribute attr = element.Attribute("readby");
                if (attr != null)
                {
                    string[] readers = attr.Value.Split('|');
                    for (int i = 0; i < readers.Length; ++i)
                    {
                        string reader = readers[i];

                        TableDescriptor.ReaderDef readerDef = null;
                        if (this.descriptor.Readers.TryGetValue(
                                reader, out readerDef) == false)
                        {
                            PrintLineError(element,
                                           "reader `{0}` is not defined", reader);
                            return(false);
                        }
                        def.Readers[reader] = readerDef;
                    }
                }
            }

            CalculateTableKeyColumnIndex(def);
            this.descriptor.Tables.Add(def);
            this.descriptor.TableNameIndex.Add(def.Name, def);

            return(true);
        }
Exemple #9
0
        public bool FilterByReader(string reader)
        {
            if (this.descriptor == null)
            {
                return(false);
            }

            if (this.descriptor.Readers.ContainsKey(reader) == false)
            {
                Console.Error.WriteLine(string.Format(
                                            "error: reader `{0}` is not defined", reader));
                return(false);
            }

            // remove unread tables
            List <TableDescriptor.TableDef> filteredTables =
                new List <TableDescriptor.TableDef>();

            for (int i = 0; i < this.descriptor.Tables.Count; ++i)
            {
                TableDescriptor.TableDef tableDef =
                    this.descriptor.Tables[i];

                if (tableDef.Readers.Count == 0 ||
                    tableDef.Readers.ContainsKey(reader))
                {
                    filteredTables.Add(tableDef);
                }
                else
                {
                    this.descriptor.TableNameIndex.Remove(tableDef.Name);
                }
            }
            this.descriptor.Tables = filteredTables;

            // remove unread columns
            for (int i = 0; i < this.descriptor.Tables.Count; ++i)
            {
                TableDescriptor.TableDef tableDef =
                    this.descriptor.Tables[i];

                List <TableDescriptor.TableDef.ColumnDef> filteredColumns =
                    new List <TableDescriptor.TableDef.ColumnDef>();
                for (int j = 0; j < tableDef.Columns.Count; ++j)
                {
                    TableDescriptor.TableDef.ColumnDef columnDef =
                        tableDef.Columns[j];
                    if (columnDef == tableDef.TableKey ||
                        columnDef.Readers.Count == 0 ||
                        columnDef.Readers.ContainsKey(reader))
                    {
                        filteredColumns.Add(columnDef);
                    }
                    else
                    {
                        tableDef.ColumnNameIndex.Remove(columnDef.Name);
                    }
                }
                tableDef.Columns = filteredColumns;
                CalculateTableKeyColumnIndex(tableDef);
            }

            // collect used structs
            Dictionary <TableDescriptor.StructDef, bool> usedStructs =
                new Dictionary <TableDescriptor.StructDef, bool>();

            for (int i = 0; i < this.descriptor.Tables.Count; ++i)
            {
                TableDescriptor.TableDef tableDef =
                    this.descriptor.Tables[i];

                for (int j = 0; j < tableDef.Columns.Count; ++j)
                {
                    TableDescriptor.TableDef.ColumnDef columnDef =
                        tableDef.Columns[j];

                    if (columnDef.RefStructDef != null)
                    {
                        usedStructs[columnDef.RefStructDef] = true;
                    }
                }
            }

            // remove unused global structs
            List <TableDescriptor.StructDef> filteredGlobalStructs =
                new List <TableDescriptor.StructDef>();

            for (int i = 0; i < this.descriptor.GlobalStructs.Count; ++i)
            {
                TableDescriptor.StructDef structDef =
                    this.descriptor.GlobalStructs[i];

                if (usedStructs.ContainsKey(structDef))
                {
                    filteredGlobalStructs.Add(structDef);
                }
                else
                {
                    this.descriptor.GlobalStructNameIndex.Remove(
                        structDef.Name);
                }
            }
            this.descriptor.GlobalStructs = filteredGlobalStructs;

            // remove unused local structs
            for (int i = 0; i < this.descriptor.Tables.Count; ++i)
            {
                TableDescriptor.TableDef tableDef =
                    this.descriptor.Tables[i];

                List <TableDescriptor.StructDef> filteredLocalStructs =
                    new List <TableDescriptor.StructDef>();
                for (int j = 0; j < tableDef.LocalStructs.Count; ++j)
                {
                    TableDescriptor.StructDef structDef =
                        tableDef.LocalStructs[j];

                    if (usedStructs.ContainsKey(structDef))
                    {
                        filteredLocalStructs.Add(structDef);
                    }
                    else
                    {
                        tableDef.LocalStructNameIndex.Remove(structDef.Name);
                    }
                }
                tableDef.LocalStructs = filteredLocalStructs;
            }

            return(true);
        }