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()); }
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; } string languageKeyword = dataTableProcessor.GetLanguageKeyword(i); if (dataTableProcessor.IsList(i)) { //stringBuilder.AppendFormat(" {0} = DataTableExtension.ParseList<{1}>(columnTexts[index++], {2}.Parse);", dataTableProcessor.GetName(i), dataTableProcessor.GetLanguageKeyword(i), dataTableProcessor.GetLanguageKeyword(i)).AppendLine(); if (dataTableProcessor.IsSystem(i) || dataTableProcessor.IsCustom(i)) { if (languageKeyword == "string") { stringBuilder.AppendFormat(" {0} = DataTableExtension.ParseList<{1}>(columnTexts[index++], (str)=>{return columnTexts[index++];});", dataTableProcessor.GetName(i), languageKeyword).AppendLine(); } else { stringBuilder.AppendFormat(" {0} = DataTableExtension.ParseList<{1}>(columnTexts[index++], {2}.Parse);", dataTableProcessor.GetName(i), languageKeyword, languageKeyword).AppendLine(); } } else if (dataTableProcessor.GetValueType(i) == DataType.Enum) { stringBuilder.AppendFormat(" {0} = DataTableExtension.ParseList<{1}>(columnTexts[index++], binaryReader.ReadEnum<EnumTest>);", dataTableProcessor.GetName(i), languageKeyword).AppendLine(); } else { stringBuilder.AppendFormat(" {0} = DataTableExtension.ParseList<{1}>(columnTexts[index++], DataTableExtension.Parse{2});", dataTableProcessor.GetName(i), dataTableProcessor.GetType(i).Name, dataTableProcessor.GetType(i).Name).AppendLine(); } } else if (dataTableProcessor.IsSystem(i) || dataTableProcessor.IsCustom(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 if (dataTableProcessor.GetValueType(i) == DataType.Enum) { stringBuilder.AppendFormat(" {0} = ({1})int.Parse(columnTexts[index++]);", dataTableProcessor.GetName(i), dataTableProcessor.GetLanguageKeyword(i)).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()); }