public void CreateCfgBytesFile(string path) { MoByteBuffer byteBuffer = new MoByteBuffer(ResDefine.CfgStreamMaxLen); MoByteBuffer tableBuffer = new MoByteBuffer(ResDefine.TabStreamMaxLen); for (int i = 0; i < _tables.Count; i++) { //写入行标记 byteBuffer.WriteShort(ResDefine.TabStreamHead); //清空缓存 tableBuffer.Clear(); //写入数据 IRow row = _tables[i].Row; for (int cellNum = row.FirstCellNum; cellNum < row.LastCellNum; cellNum++) { ICell cell = row.GetCell(cellNum); string value = GetTableCellValue(cell); HeadWrapper head = GetHead(cellNum); WriteCell(tableBuffer, head, value); } //检测数据大小有效性 int tabSize = tableBuffer.ReadableBytes(); if (tabSize == 0) { throw new Exception("Table size is zero."); } //写入到总缓存 byteBuffer.WriteInt(tabSize); byteBuffer.WriteBytes(tableBuffer.ReadBytes(tabSize)); } //创建文件 string filePath = GetSaveFileFullPath(path, ".bytes"); using (FileStream fs = new FileStream(filePath, FileMode.Create)) { byte[] data = byteBuffer.Buf; int length = byteBuffer.ReadableBytes(); fs.Write(data, 0, length); } }
private void WriteCell(MoByteBuffer buffer, HeadWrapper head, string value) { if (head.IsEmpty || head.Logo.Contains(CreateLogo) == false) { return; } //int if (head.Type == "int") { buffer.WriteInt(MoStringConvert.StringToValue <int>(value)); } else if (head.Type == "List<int>") { buffer.WriteListInt(MoStringConvert.StringToValueList <int>(value, '_')); } //long else if (head.Type == "long") { buffer.WriteLong(MoStringConvert.StringToValue <long>(value)); } else if (head.Type == "List<long>") { buffer.WriteListLong(MoStringConvert.StringToValueList <long>(value, '_')); } //float else if (head.Type == "float") { buffer.WriteFloat(MoStringConvert.StringToValue <float>(value)); } else if (head.Type == "List<float>") { buffer.WriteListFloat(MoStringConvert.StringToValueList <float>(value, '_')); } //double else if (head.Type == "double") { buffer.WriteDouble(MoStringConvert.StringToValue <double>(value)); } else if (head.Type == "List<double>") { buffer.WriteListDouble(MoStringConvert.StringToValueList <double>(value, '_')); } //bool else if (head.Type == "bool") { buffer.WriteBool(MoStringConvert.StringToBool(value)); } //string else if (head.Type == "string") { buffer.WriteUTF(value); } //enum else if (head.Type == "enumIndex") { buffer.WriteInt(MoStringConvert.StringToValue <int>(value)); } else if (head.Type == "enumName") { buffer.WriteUTF(value); } //wrapper else if (head.Type == "wrapper") { buffer.WriteUTF(value); } else if (head.Type == "List<wrapper>") { buffer.WriteUTF(value); } else { throw new Exception($"Not support head type {head.Type}"); } }