Esempio n. 1
0
        /// <summary>
        /// 解析数据
        /// </summary>
        protected override void ParseData()
        {
            MoByteBuffer bb = new MoByteBuffer(_bytes);

            int       tabLine         = 0;
            const int headMarkAndSize = 6;

            while (bb.IsReadable(headMarkAndSize))
            {
                //检测行标记
                short tabHead = bb.ReadShort();
                if (tabHead != ResDefine.TabStreamHead)
                {
                    string message = string.Format("Table stream head is invalid. File is {0} , tab line is {1}", _path, tabLine);
                    throw new Exception(message);
                }

                //检测行大小
                int tabSize = bb.ReadInt();
                if (!bb.IsReadable(tabSize) || tabSize > ResDefine.TabStreamMaxLen)
                {
                    string message = string.Format("Table stream size is invalid. File is {0}, tab line {1}", _path, tabLine);
                    throw new Exception(message);
                }

                //读取行内容
                MoCfgTab tab = null;
                try
                {
                    tab = ReadTab(bb);
                }
                catch (Exception ex)
                {
                    string message = string.Format("ReadTab falied. File is {0}, tab line {1}. Error : ", _path, tabLine, ex.ToString());
                    throw new Exception(message);
                }

                ++tabLine;

                //检测是否重复
                if (_tabs.ContainsKey(tab.Id))
                {
                    string message = string.Format("The tab key is already exist. Type is {0}, file is {1}, key is {2}", this.GetType(), _path, tab.Id);
                    throw new Exception(message);
                }
                else
                {
                    _tabs.Add(tab.Id, tab);
                }
            }
        }
Esempio n. 2
0
    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);
        }
    }
Esempio n. 3
0
 protected abstract MoCfgTab ReadTab(MoByteBuffer byteBuffer);
Esempio n. 4
0
 public abstract void ReadByte(MoByteBuffer byteBuf);
Esempio n. 5
0
    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}");
        }
    }