Ejemplo n.º 1
0
        private static string GenerateDataTableStringParser(DataTableProcessor dataTableProcessor)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder
            .AppendLine("        public override bool ParseDataRow(GameFrameworkSegment<string> dataRowSegment)")
            .AppendLine("        {")
            .AppendLine("            // Star Force 示例代码,正式项目使用时请调整此处的生成代码,以处理 GCAlloc 问题!")
            .AppendLine("            string[] columnTexts = dataRowSegment.Source.Substring(dataRowSegment.Offset, dataRowSegment.Length).Split(DataTableExtension.DataSplitSeparators);")
            .AppendLine("            for (int i = 0; i < columnTexts.Length; i++)")
            .AppendLine("            {")
            .AppendLine("                columnTexts[i] = columnTexts[i].Trim(DataTableExtension.DataTrimSeparators);")
            .AppendLine("            }")
            .AppendLine()
            .AppendLine("            int index = 0;");

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    stringBuilder.AppendLine("            index++;");
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    stringBuilder.AppendLine("            m_Id = int.Parse(columnTexts[index++]);");
                    continue;
                }

                if (dataTableProcessor.IsSystem(i))
                {
                    string languageKeyword = dataTableProcessor.GetLanguageKeyword(i);
                    if (languageKeyword == "string")
                    {
                        stringBuilder.AppendFormat("            {0} = columnTexts[index++];", dataTableProcessor.GetName(i)).AppendLine();
                    }
                    else
                    {
                        stringBuilder.AppendFormat("            {0} = {1}.Parse(columnTexts[index++]);", dataTableProcessor.GetName(i), languageKeyword).AppendLine();
                    }
                }
                else
                {
                    stringBuilder.AppendFormat("            {0} = DataTableExtension.Parse{1}(columnTexts[index++]);", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name).AppendLine();
                }
            }

            stringBuilder
            .AppendLine()
            .AppendLine("            GeneratePropertyArray();")
            .AppendLine("            return true;")
            .Append("        }");

            return(stringBuilder.ToString());
        }
Ejemplo n.º 2
0
        private static string GenerateDataTableProperties(DataTableProcessor dataTableProcessor)
        {
            StringBuilder stringBuilder = new StringBuilder();
            bool          firstProperty = true;

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    continue;
                }

                if (firstProperty)
                {
                    firstProperty = false;
                }
                else
                {
                    stringBuilder.AppendLine().AppendLine();
                }


                string typeName = dataTableProcessor.GetLanguageKeyword(i);

                if (dataTableProcessor.IsList(i))
                {
                    typeName = "List<" + dataTableProcessor.GetLanguageKeyword(i) + ">";
                }


                stringBuilder
                .AppendLine("        /// <summary>")
                .AppendFormat("        /// 获取{0}。", dataTableProcessor.GetComment(i)).AppendLine()
                .AppendLine("        /// </summary>")
                .AppendFormat("        public {0} {1}", typeName, dataTableProcessor.GetName(i)).AppendLine()
                .AppendLine("        {")
                .AppendLine("            get;")
                .AppendLine("            private set;")
                .Append("        }");
            }

            return(stringBuilder.ToString());
        }
Ejemplo n.º 3
0
        //生成重写解析字节流数据的方法
        private static string GenerateDataTableBytesParser(DataTableProcessor dataTableProcessor)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder
            .AppendLine("    public override bool ParseDataRow(GameFrameworkSegment<byte[]> dataRowSegment)")
            .AppendLine("    {")
            .AppendLine("        // Star Force 示例代码,正式项目使用时请调整此处的生成代码,以处理 GCAlloc 问题!")
            .AppendLine("        using (MemoryStream memoryStream = new MemoryStream(dataRowSegment.Source, dataRowSegment.Offset, dataRowSegment.Length, false))")
            .AppendLine("        {")
            .AppendLine("            using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8))")
            .AppendLine("            {")
            .AppendLine("                try")
            .AppendLine("                {");

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    stringBuilder.AppendLine("                    m_Id = binaryReader.ReadInt32();");
                    continue;
                }

                stringBuilder.AppendFormat("                    {0} = binaryReader.Read{1}();", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name).AppendLine();
            }

            stringBuilder
            .AppendLine("                }")
            .AppendLine("                catch (Exception e)")
            .AppendLine("                {")
            .AppendLine("                    Log.Error(\"ParseDataRow is failure, error message is:\\n{0}.\", e.ToString());")
            .AppendLine("                    return false;")
            .AppendLine("                }")
            .AppendLine("            }")
            .AppendLine("        }")
            .AppendLine()
            .AppendLine("        GeneratePropertyArray();")
            .AppendLine("        return true;")
            .Append("    }");

            return(stringBuilder.ToString());
        }
Ejemplo n.º 4
0
        //生成属性
        private static string GenerateDataTableProperties(DataTableProcessor dataTableProcessor)
        {
            StringBuilder stringBuilder = new StringBuilder();
            bool          firstProperty = true; //是否是第一条属性的标志位

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))      // 注释列
                {
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))       // 编号列
                {
                    continue;
                }

                if (firstProperty)
                {
                    firstProperty = false;
                }
                else
                {
                    stringBuilder.AppendLine().AppendLine();        //非第一条属性,要另起两行,保持与上衣属性的间距
                }

                stringBuilder
                .AppendLine("    /// <summary>")
                .AppendFormat("    /// 获取{0}", dataTableProcessor.GetComment(i)).AppendLine()           //列的注释
                .AppendLine("    /// </summary>")
                .AppendFormat("    public {0} {1} ", dataTableProcessor.GetLanguageKeyword(i), dataTableProcessor.GetName(i))
                .Append("{ get; private set; }");
                //.Append(" {")
                //.Append(" get;")
                //.Append(" private set;")
                //.Append(" }");
            }

            return(stringBuilder.ToString());
        }
        private static string GenerateDataTablePropertyArray(DataTableProcessor dataTableProcessor)
        {
            List <PropertyCollection> propertyCollections = new List <PropertyCollection>();

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    continue;
                }

                string name = dataTableProcessor.GetName(i);
                if (!EndWithNumberRegex.IsMatch(name))
                {
                    continue;
                }

                string propertyCollectionName = EndWithNumberRegex.Replace(name, string.Empty);
                int    id = int.Parse(EndWithNumberRegex.Match(name).Value);

                PropertyCollection propertyCollection = null;
                foreach (PropertyCollection pc in propertyCollections)
                {
                    if (pc.Name == propertyCollectionName)
                    {
                        propertyCollection = pc;
                        break;
                    }
                }

                if (propertyCollection == null)
                {
                    propertyCollection = new PropertyCollection(propertyCollectionName, dataTableProcessor.GetLanguageKeyword(i));
                    propertyCollections.Add(propertyCollection);
                }

                propertyCollection.AddItem(id, name);
            }

            StringBuilder stringBuilder = new StringBuilder();
            bool          firstProperty = true;

            foreach (PropertyCollection propertyCollection in propertyCollections)
            {
                if (firstProperty)
                {
                    firstProperty = false;
                }
                else
                {
                    stringBuilder.AppendLine().AppendLine();
                }

                stringBuilder
                .AppendFormat("        private KeyValuePair<int, {1}>[] m_{0} = null;", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
                .AppendLine()
                .AppendFormat("        public int {0}Count", propertyCollection.Name).AppendLine()
                .AppendLine("        {")
                .AppendLine("            get")
                .AppendLine("            {")
                .AppendFormat("                return m_{0}.Length;", propertyCollection.Name).AppendLine()
                .AppendLine("            }")
                .AppendLine("        }")
                .AppendLine()
                .AppendFormat("        public {1} Get{0}(int id)", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
                .AppendLine("        {")
                .AppendFormat("            foreach (KeyValuePair<int, {1}> i in m_{0})", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
                .AppendLine("            {")
                .AppendLine("                if (i.Key == id)")
                .AppendLine("                {")
                .AppendLine("                    return i.Value;")
                .AppendLine("                }")
                .AppendLine("            }")
                .AppendLine()
                .AppendFormat("            throw new GameFrameworkException(Utility.Text.Format(\"Get{0} with invalid id '{{0}}'.\", id.ToString()));", propertyCollection.Name).AppendLine()
                .AppendLine("        }")
                .AppendLine()
                .AppendFormat("        public {1} Get{0}At(int index)", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
                .AppendLine("        {")
                .AppendFormat("            if (index < 0 || index >= m_{0}.Length)", propertyCollection.Name).AppendLine()
                .AppendLine("            {")
                .AppendFormat("                throw new GameFrameworkException(Utility.Text.Format(\"Get{0}At with invalid index '{{0}}'.\", index.ToString()));", propertyCollection.Name).AppendLine()
                .AppendLine("            }")
                .AppendLine()
                .AppendFormat("            return m_{0}[index].Value;", propertyCollection.Name).AppendLine()
                .Append("        }");
            }

            if (propertyCollections.Count > 0)
            {
                stringBuilder.AppendLine().AppendLine();
            }

            stringBuilder
            .AppendLine("        private void GeneratePropertyArray()")
            .AppendLine("        {");

            firstProperty = true;
            foreach (PropertyCollection propertyCollection in propertyCollections)
            {
                if (firstProperty)
                {
                    firstProperty = false;
                }
                else
                {
                    stringBuilder.AppendLine().AppendLine();
                }

                stringBuilder
                .AppendFormat("            m_{0} = new KeyValuePair<int, {1}>[]", propertyCollection.Name, propertyCollection.LanguageKeyword).AppendLine()
                .AppendLine("            {");

                int itemCount = propertyCollection.ItemCount;
                for (int i = 0; i < itemCount; i++)
                {
                    KeyValuePair <int, string> item = propertyCollection.GetItem(i);
                    stringBuilder.AppendFormat("                new KeyValuePair<int, {0}>({1}, {2}),", propertyCollection.LanguageKeyword, item.Key.ToString(), item.Value).AppendLine();
                }

                stringBuilder.Append("            };");
            }

            stringBuilder
            .AppendLine()
            .Append("        }");

            return(stringBuilder.ToString());
        }
        private static string GenerateDataTableParser(DataTableProcessor dataTableProcessor)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder
            .AppendLine("        public override bool ParseDataRow(GameFrameworkDataSegment dataRowSegment, object dataTableUserData)")
            .AppendLine("        {")
            .AppendLine("            Type dataType = dataRowSegment.DataType;")
            .AppendLine("            if (dataType == typeof(string))")
            .AppendLine("            {")
            .AppendLine("                string[] columnTexts = ((string)dataRowSegment.Data).Substring(dataRowSegment.Offset, dataRowSegment.Length).Split(DataTableExtension.DataSplitSeparators);")
            .AppendLine("                for (int i = 0; i < columnTexts.Length; i++)")
            .AppendLine("                {")
            .AppendLine("                    columnTexts[i] = columnTexts[i].Trim(DataTableExtension.DataTrimSeparators);")
            .AppendLine("                }")
            .AppendLine()
            .AppendLine("                int index = 0;");

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    stringBuilder.AppendLine("                index++;");
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    stringBuilder.AppendLine("                m_Id = int.Parse(columnTexts[index++]);");
                    continue;
                }

                if (dataTableProcessor.IsSystem(i))
                {
                    string languageKeyword = dataTableProcessor.GetLanguageKeyword(i);
                    if (languageKeyword == "string")
                    {
                        stringBuilder.AppendFormat("                {0} = columnTexts[index++];", dataTableProcessor.GetName(i)).AppendLine();
                    }
                    else
                    {
                        stringBuilder.AppendFormat("                {0} = {1}.Parse(columnTexts[index++]);", dataTableProcessor.GetName(i), languageKeyword).AppendLine();
                    }
                }
                else
                {
                    stringBuilder.AppendFormat("                {0} = DataTableExtension.Parse{1}(columnTexts[index++]);", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name).AppendLine();
                }
            }

            stringBuilder
            .AppendLine("            }")
            .AppendLine("            else if (dataType == typeof(byte[]))")
            .AppendLine("            {")
            .AppendLine("                string[] strings = (string[])dataTableUserData;")
            .AppendLine("                using (MemoryStream memoryStream = new MemoryStream((byte[])dataRowSegment.Data, dataRowSegment.Offset, dataRowSegment.Length, false))")
            .AppendLine("                {")
            .AppendLine("                    using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8))")
            .AppendLine("                    {");

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    stringBuilder.AppendLine("                        m_Id = binaryReader.Read7BitEncodedInt32();");
                    continue;
                }

                string languageKeyword = dataTableProcessor.GetLanguageKeyword(i);
                if (languageKeyword == "int" || languageKeyword == "uint" || languageKeyword == "long" || languageKeyword == "ulong")
                {
                    stringBuilder.AppendFormat("                        {0} = binaryReader.Read7BitEncoded{1}();", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name).AppendLine();
                }
                else if (languageKeyword == "string")
                {
                    stringBuilder.AppendFormat("                        {0} = strings[binaryReader.Read7BitEncodedInt32()];", dataTableProcessor.GetName(i)).AppendLine();
                }
                else
                {
                    stringBuilder.AppendFormat("                        {0} = binaryReader.Read{1}();", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name).AppendLine();
                }
            }

            stringBuilder
            .AppendLine("                    }")
            .AppendLine("                }")
            .AppendLine("            }")
            .AppendLine("            else")
            .AppendLine("            {")
            .AppendLine("                Log.Warning(\"Can not parse data row which type '{0}' is invalid.\", dataType.FullName);")
            .AppendLine("                return false;")
            .AppendLine("            }")
            .AppendLine()
            .AppendLine("            GeneratePropertyArray();")
            .AppendLine("            return true;")
            .Append("        }");

            return(stringBuilder.ToString());
        }
Ejemplo n.º 7
0
        private static string GenerateDataTableBytesParser(DataTableProcessor dataTableProcessor)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder
            .AppendLine("        public override bool ParseDataRow(GameFrameworkSegment<byte[]> dataRowSegment)")
            .AppendLine("        {")
            .AppendLine("            // Star Force 示例代码,正式项目使用时请调整此处的生成代码,以处理 GCAlloc 问题!")
            .AppendLine("            using (MemoryStream memoryStream = new MemoryStream(dataRowSegment.Source, dataRowSegment.Offset, dataRowSegment.Length, false))")
            .AppendLine("            {")
            .AppendLine("                using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8))")
            .AppendLine("                {");

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    stringBuilder.AppendLine("                    m_Id = binaryReader.ReadInt32();");
                    continue;
                }

                string languageKeyword = dataTableProcessor.GetLanguageKeyword(i);

                if (dataTableProcessor.IsList(i))
                {
                    //if (dataTableProcessor.IsSystem(i) || dataTableProcessor.IsCustom(i))
                    //{

                    //    stringBuilder.AppendFormat("                    {0} = binaryReader.ReadList<{1}>(binaryReader.Read{2});", dataTableProcessor.GetName(i), languageKeyword, dataTableProcessor.GetType(i).Name).AppendLine();
                    //}
                    //else
                    if (dataTableProcessor.GetValueType(i) == DataType.Enum)
                    {
                        stringBuilder.AppendFormat("                    {0} = binaryReader.ReadList<{1}>(binaryReader.ReadInt32);", dataTableProcessor.GetName(i), languageKeyword).AppendLine();
                    }
                    else
                    {
                        stringBuilder.AppendFormat("                    {0} = binaryReader.ReadList<{1}>(binaryReader.Read{2});", dataTableProcessor.GetName(i), languageKeyword, dataTableProcessor.GetType(i).Name).AppendLine();
                    }
                }
                else if (dataTableProcessor.GetValueType(i) == DataType.Enum)
                {
                    stringBuilder.AppendFormat("                    {0} = ({1})binaryReader.ReadInt32();", dataTableProcessor.GetName(i), languageKeyword).AppendLine();
                }
                else
                {
                    stringBuilder.AppendFormat("                    {0} = binaryReader.Read{1}();", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name).AppendLine();
                }
            }

            stringBuilder
            .AppendLine("                }")
            .AppendLine("            }")
            .AppendLine()
            .AppendLine("            GeneratePropertyArray();")
            .AppendLine("            return true;")
            .Append("        }");

            return(stringBuilder.ToString());
        }