Ejemplo n.º 1
0
    /// <summary>
    /// 生成加密后的文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name="dt"></param>
    protected override void CreateData(string path, DataTable dt, bool ifCreateCode = true)
    {
        //数据格式 行数 列数 二维数组每项的值 这里不做判断 都用string存储

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

        //指定文件夹目录
        string dir = filePath.Substring(0, filePath.LastIndexOf('/'));

        dir = dir.Substring(dir.LastIndexOf('/') + 1) + "/";

        if (dir == "Excel/")
        {
            dir = "";
        }

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

        using (Game_MemoryStream ms = new Game_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步:写入文件
        //------------------
        string dataPath  = Application.dataPath;
        string buildPath = dataPath + "/StreamingAssets/AutoCreate/" + dir;

        if (!Directory.Exists(buildPath))
        {
            Directory.CreateDirectory(buildPath);
        }

        //Debug.Log(fileName + "  写入目录:" + buildPath);

        FileStream fs = new FileStream(string.Format("{0}{1}", buildPath, fileName + ".data"), FileMode.Create);

        fs.Write(buffer, 0, buffer.Length);
        fs.Close();

        if (ifCreateCode)
        {
            string codePath = dataPath + "/Scripts/FileDataSystem/Data/";
            CreateEntity(codePath, fileName, dir, dataArr);
            CreateDBModel(codePath, fileName, dir, dataArr);
        }
    }
Ejemplo n.º 2
0
        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步:读取文件
            //------------------

#if  UNITY_EDITOR
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
            }
#elif UNITY_ANDROID && !UNITY_EDITOR
            WWW www = new WWW(path);    //WWW会自动开始读取文件
            while (!www.isDone)
            {
            }                                    //WWW是异步读取,所以要用循环来等待
            buffer = www.bytes;                  //存到字节数组里
#elif UNITY_IPHONE && !UNITY_EDITOR
            WWW www = new WWW("file://" + path); //WWW会自动开始读取文件
            while (!www.isDone)
            {
            }                       //WWW是异步读取,所以要用循环来等待
            buffer = www.bytes;     //存到字节数组里
#endif

            //------------------
            //第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 (Game_MemoryStream ms = new Game_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;

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