Exemple #1
0
    /// <summary>
    /// 封装数据包
    /// </summary>
    /// <param name="buffer"></param>
    private byte[] MakeDataPackage(byte[] buffer)
    {
        //1.压缩
        bool isCompress = buffer.Length > m_CompressLen;

        if (isCompress)
        {
            buffer = ZlibHelper.CompressBytes(buffer);
        }

        //2.异或
        buffer = SecurityUtil.Xor(buffer);

        //3.计算CRC
        ushort crc = Crc16.CalculateCrc16(buffer);

        byte[] retData;
        using (MMO_MemoryStream ms = new MMO_MemoryStream())
        {
            ms.WriteUShort((ushort)(buffer.Length + 3));
            ms.WriteBool(isCompress);
            ms.WriteUShort(crc);
            ms.Write(buffer, 0, buffer.Length);
            retData = ms.ToArray();
        }
        return(retData);
    }
Exemple #2
0
        /// <summary>
        /// 封装数据包
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private byte[] MakeData(byte[] data)
        {
            byte[] retBuffer = null;

            //1.如果数据包的长度 大于了m_CompressLen 则进行压缩
            bool isCompress = data.Length > m_CompressLen ? true : false;

            if (isCompress)
            {
                data = ZlibHelper.CompressBytes(data);
            }

            //2.异或
            data = SecurityUtil.Xor(data);

            //3.Crc校验 压缩后的
            ushort crc = Crc16.CalculateCrc16(data);

            Console.WriteLine("crc=" + crc);
            MMO_MemoryStream ms = this.m_SocketSendMS;

            ms.SetLength(0);

            ms.WriteUShort((ushort)(data.Length + 3));
            ms.WriteBool(isCompress);
            ms.WriteUShort(crc);
            ms.Write(data, 0, data.Length);

            retBuffer = ms.ToArray();
            return(retBuffer);
        }
Exemple #3
0
    /// <summary>
    /// 封装数据包
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    private byte[] MakeData(byte[] data)
    {
        byte[] retBuffer = null;
        //0.压缩标志 //数据包长度大于设定长度则压缩
        bool isCompress = data.Length > m_CompressLength ? true : false;

        if (isCompress)
        {
            data = ZlibHelper.CompressBytes(data);
        }

        //1.异或
        data = SecurityUtil.Xor(data);

        //2.CRC校验
        ushort crc = Crc16.CalculateCrc16(data);

        using (MMO_MemoryStream ms = new MMO_MemoryStream())
        {
            //3.写入包长度
            //往ms中写入数据(包长度),数据的长度+压缩标志(1byte)+CRC校验(UShort(2byte))
            ms.WriteUShort((ushort)(data.Length + 3));
            //往ms中写入数据(压缩标识)
            ms.WriteBool(isCompress);
            ms.WriteUShort(crc);
            ms.Write(data, 0, data.Length);

            retBuffer = ms.ToArray();
        }

        return(retBuffer);
    }
Exemple #4
0
    /// <summary>
    /// 沾包,加密数据包
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public byte[] MakeData(byte[] data)
    {
        byte[] retBuffer = null;
        //1、是否压缩
        bool IsComPressLen = data.Length > mComPressLen ? true : false;

        if (IsComPressLen)
        {
            //压缩
            data = ZlibHelper.CompressBytes(data);
        }
        //2、异或
        data = SecurityUtil.Xor(data);
        //3、crc校验
        ushort crc = CRC16.CalculateCrc16(data);

        using (MMO_MemoryStream ms = new MMO_MemoryStream())
        {
            //1、包头(+3==Crc(2字节)+IsComPressLen(2字节))
            ms.WriteUShort((ushort)(data.Length + 3));
            ms.WriteBool(IsComPressLen);
            ms.WriteUShort(crc);
            ms.Write(data, 0, data.Length);
            retBuffer = ms.ToArray();
        }
        return(retBuffer);
    }
Exemple #5
0
        /// <summary>
        /// 封装数据包
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private byte[] MakeData(byte[] data)
        {
            byte[] retBuffer = null;

            //1.长度压缩判断
            bool isCompress = data.Length > m_CompressLen ? true : false;

            if (isCompress)
            {
                //2.进行压缩
                data = ZlibHelper.CompressBytes(data);
            }

            //3.异或
            data = SecurityUtil.Xor(data);
            //4.异或因子 Crc 校验码
            ushort crc = Crc16.CalculateCrc16(data);

            Console.WriteLine("crc=" + crc);

            //写入
            using (MMO_MemoryStream ms = new MMO_MemoryStream())
            {
                ms.WriteUShort((ushort)(data.Length + 3)); //数据长度
                ms.WriteBool(isCompress);                  //压缩判断
                ms.WriteUShort(crc);                       //异或
                ms.Write(data, 0, data.Length);            //数据写入
                retBuffer = ms.ToArray();
            }
            return(retBuffer);
        }
Exemple #6
0
    /// <summary>
    /// 封装数据包
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    private byte[] MakeData(byte[] data)
    {
        byte[] retBuffer = null;

        //1.如果数据包的长度 大于了m_CompressLen 则进行压缩
        bool isCompress = data.Length > m_CompressLen ? true : false;

        if (isCompress)
        {
            data = ZlibHelper.CompressBytes(data);
        }

        //2.异或
        data = SecurityUtil.Xor(data);

        //3.Crc校验 压缩后的
        ushort crc = Crc16.CalculateCrc16(data);

        using (MMO_MemoryStream ms = new MMO_MemoryStream())
        {
            ms.WriteUShort((ushort)(data.Length + 3));
            ms.WriteBool(isCompress);
            ms.WriteUShort(crc);
            ms.Write(data, 0, data.Length);

            retBuffer = ms.ToArray();
        }
        return(retBuffer);
    }
Exemple #7
0
        /// <summary>
        /// 创建包数据:包头+包体
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        private byte[] MakeData(byte[] buffer)
        {
            //1.压缩
            bool isCompress = buffer.Length > m_CompressLen ? true : false;

            if (isCompress)
            {
                buffer = ZlibHelper.CompressBytes(buffer);
            }
            //2.CRC1校验
            ushort crc = Crc16.CalculateCrc16(buffer);

            Console.WriteLine(crc);
            //3.异或
            buffer = SecurityUtil.Xor(buffer);

            using (MMO_MemoryStream ms = new MMO_MemoryStream())
            {
                //包头长度 压缩标识1+CRC16校验+buffer的长度
                ms.WriteUShort((ushort)(buffer.Length + 3));
                ms.WriteBool(isCompress);
                ms.WriteUShort(crc);
                ms.Write(buffer, 0, buffer.Length);
                return(ms.ToArray());
            }
        }
Exemple #8
0
        // 封装数据包
        byte[] MakeData(byte[] data)
        {
            byte[] retBuffer = null;
            // 1.1 压缩
            bool isCompress = data.Length > m_CompressLen;

            if (isCompress)
            {
                data = ZlibHelper.CompressBytes(data);
            }
            // 2 异或加密
            data = SecurityUtil.Xor(data);
            // 3 加验证
            ushort crc = Crc16.CalculateCrc16(data);

            using (MMO_MemoryStream ms = new MMO_MemoryStream())
            {
                ms.WriteUShort((ushort)(data.Length + 3));
                ms.WriteBool(isCompress);
                ms.WriteUShort(crc);
                ms.Write(data, 0, data.Length);

                retBuffer = ms.ToArray();
            }

            return(retBuffer);
        }
    // 加密并保存文本
    static bool EncryptAndSave()
    {
        //Debug.Log("加密并保存文本");
        byte[] buffer = Encoding.UTF8.GetBytes(m_EncryptStr);
        buffer = SecurityUtil.Xor(buffer);
        buffer = ZlibHelper.CompressBytes(buffer);

        //Debug.Log(m_Path);
        IOUtil.CreateTextFile(m_Path, buffer);
        return(true);
    }
Exemple #10
0
        public void Test_Zlib_Compress()
        {
            var compressCount = 4;

            for (int i = 0; i <= compressCount; i++)
            {
                var json = FileHelper.Read(i == 0
                    ? "D:\\iTestRunner_R1_format.txt"
                    : $"D:\\test_compression_result_{i - 1}.txt");
                var initBytes = Encoding.UTF8.GetBytes(json);
                var result    = ZlibHelper.CompressBytes(initBytes, zlibConst.Z_BEST_COMPRESSION);
                FileHelper.Write($"D:\\test_compression_result_{i}.txt", result);
            }
        }
Exemple #11
0
    /// <summary>
    /// 制作一个数据包,该数据包是符合传给服务器的格式的(包头+包体)
    /// 当前工程的格式是包头:包体长度,包体:压缩标志+CRC校验码+加密的真正数据(异或算法)(协议编码+协议内容)
    /// 把private改为public,配合本机模式本地测试用
    /// </summary>
    /// <param name="data">真实数据</param>
    /// <returns></returns>
    public byte[] MakeDataPkg(byte[] data)
    {
        byte[] returnBuffer = null;
        data = SecurityUtil.XorAlgorithm(data);           //1、加密
        bool isCompress = data.Length > mCompressMinSize; //2、压缩

        if (isCompress)
        {
            //开始压缩
            data = ZlibHelper.CompressBytes(data);
        }
        ushort crc16 = Crc16.CalculateCrc16(data);  //3、CRC校验

        using (MemoryStreamUtil stream = new MemoryStreamUtil())
        {
            stream.WriteUShort((ushort)(data.Length + 3)); //写包头,+3是因为多了一个bool,一个ushort,一共3字节
            stream.WriteBool(isCompress);                  //写压缩标志
            stream.WriteUShort(crc16);                     //写CRC
            stream.Write(data, 0, data.Length);            //写加密后的真实数据
            returnBuffer = stream.ToArray();
        }
        Debug.Log("数据包构建完毕!");
        return(returnBuffer);
    }
Exemple #12
0
    /// <summary>
    /// 封装数据包。
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    private byte[] makeData(byte[] data)
    {
        bool isCompress = false;

        if (data.Length > 200)
        {
            isCompress = true;
            data       = ZlibHelper.CompressBytes(data);
        }
        data = SecurityUtil.Xor(data);               //加密
        ushort crc     = Crc16.CalculateCrc16(data); //校验码。
        ushort bodyLen = (ushort)(data.Length + 3);  //包体长度

        MyMemoryStream m = new MyMemoryStream();

        m.WriteUShort(bodyLen);
        m.WriteBool(isCompress);
        m.WriteUShort(crc);
        m.Write(data, 0, data.Length);
        data = m.ToArray();
        m.Close();

        return(data);
    }
Exemple #13
0
    /// <summary>
    /// 生成依赖关系文件
    /// </summary>
    private void OnCreateDependenciesFile()
    {
        //第一次循环 把所有的Asset存储到一个列表里

        //临时列表
        List <AssetEntity> tempLst = new List <AssetEntity>();

        //循环设置文件夹包括子文件里边的项
        for (int i = 0; i < m_List.Count; i++)
        {
            AssetBundleEntity entity = m_List[i];//取到一个节点

            string[] folderArr = new string[entity.PathList.Count];
            for (int j = 0; j < entity.PathList.Count; j++)
            {
                string path = Application.dataPath + "/" + entity.PathList[j];
                //Debug.LogError("path=" + path);
                CollectFileInfo(tempLst, path);
            }
        }

        //
        int len = tempLst.Count;

        //资源列表
        List <AssetEntity> assetList = new List <AssetEntity>();

        for (int i = 0; i < len; i++)
        {
            AssetEntity entity = tempLst[i];

            AssetEntity newEntity = new AssetEntity();
            newEntity.Category        = entity.Category;
            newEntity.AssetName       = entity.AssetFullName.Substring(entity.AssetFullName.LastIndexOf("/") + 1);
            newEntity.AssetName       = newEntity.AssetName.Substring(0, newEntity.AssetName.LastIndexOf("."));
            newEntity.AssetFullName   = entity.AssetFullName;
            newEntity.AssetBundleName = entity.AssetBundleName;

            assetList.Add(newEntity);

            //场景不需要检查依赖项
            if (entity.Category == AssetCategory.Scenes)
            {
                continue;
            }

            newEntity.DependsAssetList = new List <AssetDependsEntity>();

            string[] arr = AssetDatabase.GetDependencies(entity.AssetFullName);
            foreach (string str in arr)
            {
                if (!str.Equals(newEntity.AssetFullName, StringComparison.CurrentCultureIgnoreCase) && GetIsAsset(tempLst, str))
                {
                    AssetDependsEntity assetDepends = new AssetDependsEntity();
                    assetDepends.Category      = GetAssetCategory(str);
                    assetDepends.AssetFullName = str;

                    //把依赖资源 加入到依赖资源列表
                    newEntity.DependsAssetList.Add(assetDepends);
                }
            }
        }

        //生成一个Json文件
        string targetPath = Application.dataPath + "/../AssetBundles/" + dal.GetVersion() + "/" + arrBuildTarget[buildTargetIndex];

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

        string strJsonFilePath = targetPath + "/AssetInfo.json"; //版本文件路径

        IOUtil.CreateTextFile(strJsonFilePath, LitJson.JsonMapper.ToJson(assetList));
        Debug.Log("生成 AssetInfo.json 完毕");

        MMO_MemoryStream ms = new MMO_MemoryStream();

        //生成二进制文件
        len = assetList.Count;
        ms.WriteInt(len);

        for (int i = 0; i < len; i++)
        {
            AssetEntity entity = assetList[i];
            ms.WriteByte((byte)entity.Category);
            ms.WriteUTF8String(entity.AssetFullName);
            ms.WriteUTF8String(entity.AssetBundleName);

            if (entity.DependsAssetList != null)
            {
                //添加依赖资源
                int depLen = entity.DependsAssetList.Count;
                ms.WriteInt(depLen);
                for (int j = 0; j < depLen; j++)
                {
                    AssetDependsEntity assetDepends = entity.DependsAssetList[j];
                    ms.WriteByte((byte)assetDepends.Category);
                    ms.WriteUTF8String(assetDepends.AssetFullName);
                }
            }
            else
            {
                ms.WriteInt(0);
            }
        }

        string filePath = targetPath + "/AssetInfo.bytes"; //版本文件路径

        byte[] buffer = ms.ToArray();
        buffer = ZlibHelper.CompressBytes(buffer);
        FileStream fs = new FileStream(filePath, FileMode.Create);

        fs.Write(buffer, 0, buffer.Length);
        fs.Close();
        fs.Dispose();
        Debug.Log("生成 AssetInfo.bytes 完毕");
    }
Exemple #14
0
    /// <summary>
    /// 生成版本文件
    /// </summary>
    private void OnCreateVersionFileCallBack()
    {
        string path = Application.dataPath + "/../AssetBundles/" + dal.GetVersion() + "/" + arrBuildTarget[buildTargetIndex];

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

        string strVersionFilePath = path + "/VersionFile.txt"; //版本文件路径

        //如果版本文件存在 则删除
        IOUtil.DeleteFile(strVersionFilePath);

        StringBuilder sbContent = new StringBuilder();

        DirectoryInfo directory = new DirectoryInfo(path);

        //拿到文件夹下所有文件
        FileInfo[] arrFiles = directory.GetFiles("*", SearchOption.AllDirectories);

        sbContent.AppendLine(dal.GetVersion());
        for (int i = 0; i < arrFiles.Length; i++)
        {
            FileInfo file = arrFiles[i];

            if (file.Extension == ".manifest")
            {
                continue;
            }
            string fullName = file.FullName; //全名 包含路径扩展名

            //相对路径
            string name = fullName.Substring(fullName.IndexOf(arrBuildTarget[buildTargetIndex]) + arrBuildTarget[buildTargetIndex].Length + 1);

            string md5 = EncryptUtil.GetFileMD5(fullName); //文件的MD5
            if (md5 == null)
            {
                continue;
            }

            string size = file.Length.ToString(); //文件大小

            bool isFirstData = false;             //是否初始数据
            bool isEncrypt   = false;
            bool isBreak     = false;

            for (int j = 0; j < m_List.Count; j++)
            {
                foreach (string xmlPath in m_List[j].PathList)
                {
                    string tempPath = xmlPath;
                    if (xmlPath.IndexOf(".") != -1)
                    {
                        tempPath = xmlPath.Substring(0, xmlPath.IndexOf("."));
                    }

                    name = name.Replace("\\", "/");
                    if (name.IndexOf(tempPath, StringComparison.CurrentCultureIgnoreCase) != -1)
                    {
                        isFirstData = m_List[j].IsFirstData;
                        isEncrypt   = m_List[j].IsEncrypt;
                        isBreak     = true;
                        break;
                    }
                }
                if (isBreak)
                {
                    break;
                }
            }

            string strLine = string.Format("{0}|{1}|{2}|{3}|{4}", name, md5, size, isFirstData ? 1 : 0, isEncrypt ? 1 : 0);
            sbContent.AppendLine(strLine);
        }

        IOUtil.CreateTextFile(strVersionFilePath, sbContent.ToString());

        MMO_MemoryStream ms  = new MMO_MemoryStream();
        string           str = sbContent.ToString().Trim();

        string[] arr = str.Split('\n');
        int      len = arr.Length;

        ms.WriteInt(len);
        for (int i = 0; i < len; i++)
        {
            if (i == 0)
            {
                ms.WriteUTF8String(arr[i]);
            }
            else
            {
                string[] arrInner = arr[i].Split('|');
                ms.WriteUTF8String(arrInner[0]);
                ms.WriteUTF8String(arrInner[1]);
                ms.WriteULong(ulong.Parse(arrInner[2]));
                ms.WriteByte(byte.Parse(arrInner[3]));
                ms.WriteByte(byte.Parse(arrInner[4]));
            }
        }

        string filePath = path + "/VersionFile.bytes"; //版本文件路径

        byte[] buffer = ms.ToArray();
        buffer = ZlibHelper.CompressBytes(buffer);
        FileStream fs = new FileStream(filePath, FileMode.Create);

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

        Debug.Log("创建版本文件成功");
    }
Exemple #15
0
    public static void AssetBundleCopyToStreamingAsstes()
    {
        string toPath = Application.streamingAssetsPath + "/AssetBundles/";

        if (Directory.Exists(toPath))
        {
            Directory.Delete(toPath, true);
        }
        Directory.CreateDirectory(toPath);

        IOUtil.CopyDirectory(Application.persistentDataPath, toPath);

        //重新生成版本文件
        //1.先读取persistentDataPath里边的版本文件 这个版本文件里 存放了所有的资源包信息

        byte[] buffer  = IOUtil.GetFileBuffer(Application.persistentDataPath + "/VersionFile.bytes");
        string version = "";
        Dictionary <string, AssetBundleInfoEntity> dic    = ResourceManager.GetAssetBundleVersionList(buffer, ref version);
        Dictionary <string, AssetBundleInfoEntity> newDic = new Dictionary <string, AssetBundleInfoEntity>();

        DirectoryInfo directory = new DirectoryInfo(toPath);

        //拿到文件夹下所有文件
        FileInfo[] arrFiles = directory.GetFiles("*", SearchOption.AllDirectories);

        for (int i = 0; i < arrFiles.Length; i++)
        {
            FileInfo file     = arrFiles[i];
            string   fullName = file.FullName.Replace("\\", "/"); //全名 包含路径扩展名
            string   name     = fullName.Replace(toPath, "");
            if (name.Equals("AssetInfo.json", StringComparison.CurrentCultureIgnoreCase) ||
                name.Equals("Windows", StringComparison.CurrentCultureIgnoreCase) ||
                name.Equals("Windows.manifest", StringComparison.CurrentCultureIgnoreCase)

                || name.Equals("Android", StringComparison.CurrentCultureIgnoreCase) ||
                name.Equals("Android.manifest", StringComparison.CurrentCultureIgnoreCase)

                || name.Equals("iOS", StringComparison.CurrentCultureIgnoreCase) ||
                name.Equals("iOS.manifest", StringComparison.CurrentCultureIgnoreCase)
                )
            {
                File.Delete(file.FullName);
                continue;
            }

            AssetBundleInfoEntity entity = null;
            dic.TryGetValue(name, out entity);


            if (entity != null)
            {
                newDic[name] = entity;
            }
        }

        StringBuilder sbContent = new StringBuilder();

        sbContent.AppendLine(version);

        foreach (var item in newDic)
        {
            AssetBundleInfoEntity entity = item.Value;
            string strLine = string.Format("{0}|{1}|{2}|{3}|{4}", entity.AssetBundleName, entity.MD5, entity.Size, entity.IsFirstData ? 1 : 0, entity.IsEncrypt ? 1 : 0);
            sbContent.AppendLine(strLine);
        }

        IOUtil.CreateTextFile(toPath + "VersionFile.txt", sbContent.ToString());

        //=======================
        MMO_MemoryStream ms  = new MMO_MemoryStream();
        string           str = sbContent.ToString().Trim();

        string[] arr = str.Split('\n');
        int      len = arr.Length;

        ms.WriteInt(len);
        for (int i = 0; i < len; i++)
        {
            if (i == 0)
            {
                ms.WriteUTF8String(arr[i]);
            }
            else
            {
                string[] arrInner = arr[i].Split('|');
                ms.WriteUTF8String(arrInner[0]);
                ms.WriteUTF8String(arrInner[1]);
                ms.WriteULong(ulong.Parse(arrInner[2]));
                ms.WriteByte(byte.Parse(arrInner[3]));
                ms.WriteByte(byte.Parse(arrInner[4]));
            }
        }

        string filePath = toPath + "/VersionFile.bytes"; //版本文件路径

        buffer = ms.ToArray();
        buffer = ZlibHelper.CompressBytes(buffer);
        FileStream fs = new FileStream(filePath, FileMode.Create);

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

        AssetDatabase.Refresh();
        Debug.Log("初始资源拷贝到StreamingAsstes完毕");
    }
Exemple #16
0
        /// <summary>
        /// 创建数据
        /// </summary>
        /// <param name="path"></param>
        /// <param name="table"></param>
        private void CreateData(string path, DataTable table)
        {
            //文件路径
            string filePath = path.Substring(0, path.LastIndexOf("\\") + 1);
            //文件完整名字
            string fileFullName = path.Substring(path.LastIndexOf("\\") + 1);
            //文件名
            string fileName = fileFullName.Substring(0, fileFullName.LastIndexOf("."));

            int row     = table.Rows.Count;
            int columns = table.Columns.Count;

            byte[] buffer = null;
            //头三行用来做脚本创建
            string[,] titleStr = null;
            using (MMO_MemoryStream ms = new MMO_MemoryStream())
            {
                //写入行 列数
                ms.WriteInt(row);
                ms.WriteInt(columns);

                titleStr = new string[columns, 3];

                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        //头三行 用来脚本信息
                        if (i < 3)
                        {
                            titleStr[j, i] = table.Rows[i][j].ToString().Trim();
                        }
                        ms.WriteUTF8String(table.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);


            //------------------
            ////第2步:写入文件
            ////------------------
            FileStream fs = new FileStream(string.Format("{0}{1}.data", filePath, fileName), FileMode.Create);

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


            CreateEntity(filePath, fileName, titleStr);
            CreateDBModel(filePath, fileName, titleStr);
        }
Exemple #17
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);
        }
    }
Exemple #18
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);
        }