示例#1
0
        /// <summary>
        /// 生成加密后的文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="dt"></param>
        private void CreateData(string path, DataTable dt, string tempFileName)
        {
            //数据格式 行数 列数 二维数组每项的值 这里不做判断 都用string存储

            string filePath     = path.Substring(0, path.LastIndexOf('\\') + 1);
            string fileFullName = path.Substring(path.LastIndexOf('\\') + 1);
            //string fileName = fileFullName.Substring(0, fileFullName.LastIndexOf('.'));
            string fileName = tempFileName;

            byte[] buffer = null;
            string[,] dataArr = null;

            using (MMO_MemoryStream ms = new MMO_MemoryStream())
            {
                int row     = dt.Rows.Count;
                int columns = dt.Columns.Count;

                dataArr = new string[columns, 3];

                ms.WriteInt(row);
                ms.WriteInt(columns);
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        if (i < 3)
                        {
                            dataArr[j, i] = dt.Rows[i][j].ToString().Trim();
                        }

                        ms.WriteUTF8String(dt.Rows[i][j].ToString().Trim());
                    }
                }
                buffer = ms.ToArray();
            }

            //------------------
            //第1步:xor加密
            //------------------
            int iScaleLen = xorScale.Length;

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)(buffer[i] ^ xorScale[i % iScaleLen]);
            }

            //------------------
            //第2步:压缩
            //------------------
            //压缩后的字节流
            buffer = ZlibHelper.CompressBytes(buffer);

            //------------------
            //第3步:写入文件
            //------------------
            if (!Directory.Exists(m_CreateDataPath))
            {
                Directory.CreateDirectory(m_CreateDataPath);
            }
            FileStream fs = new FileStream(string.Format("{0}{1}", m_CreateDataPath, fileName + ".data"), FileMode.Create);

            //FileStream fs = new FileStream(string.Format("{0}{1}", filePath, fileName + ".data"), FileMode.Create);
            fs.Write(buffer, 0, buffer.Length);
            fs.Close();

            CreateEntity(m_CreateCsOrLuaPath, fileName, dataArr);
            CreateDBModel(m_CreateCsOrLuaPath, fileName, dataArr);
        }
        private byte[] xorScale = new byte[] { 45, 66, 38, 55, 23, 254, 9, 165, 90, 19, 41, 45, 201, 58, 55, 37, 254, 185, 165, 169, 19, 171 };//.data文件的xor加解密因子
        #endregion

        #region GameDataTableParser 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="path"></param>
        public GameDataTableParser(string path)
        {
            m_FieldNameDic = new Dictionary <string, int>();
            byte[] buffer = null;

            //------------------
            //第1步:读取文件
            //------------------
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
            }

            //------------------
            //第2步:解压缩
            //------------------
            buffer = ZlibHelper.DeCompressBytes(buffer);

            //------------------
            //第3步:xor解密
            //------------------
            int iScaleLen = xorScale.Length;

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)(buffer[i] ^ xorScale[i % iScaleLen]);
            }

            //------------------
            //第4步:解析数据到数组
            //------------------
            using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer))
            {
                m_Row    = ms.ReadInt();
                m_Column = ms.ReadInt();

                m_GameData  = new String[m_Row, m_Column];
                m_FieldName = new string[m_Column];

                for (int i = 0; i < m_Row; i++)
                {
                    for (int j = 0; j < m_Column; j++)
                    {
                        string str = ms.ReadUTF8String();

                        if (i == 0)
                        {
                            //表示读取的是字段
                            m_FieldName[j]      = str;
                            m_FieldNameDic[str] = j;
                        }
                        else if (i > 2)
                        {
                            //表示读取的是内容
                            m_GameData[i, j] = str;
                        }
                    }
                }
            }
        }